Saturday, October 30, 2010

Re: current page info

On Sat, Oct 30, 2010 at 6:49 PM, grandioso
<something.like.a.phenomenon@gmail.com> wrote:
> I feel really stupid for asking this, but I'm just noob baker, so...
> How do I add a "selected" class to a menu item that points to the
> current page and how do I create a title for the current page ?
>
> I created a default layout, and all the pages. Then I created the
> routes in 'app/config/routes.php' like "Router::connect('/contact',
> array('controller' => 'pages', 'action' => 'display', 'contact'));".
> It works just fine, but I have no idea how to add "selected" to the
> currently selected menu item and how to create a custom title for the
> page.
>
> It would be great if I could just pass the "title" and "selected"
> string in this array.
> Is it possible ?

$this->set('title_for_layout', 'some title');

I have this in AppController:

protected function _setPageTitle($title)
{
$title = 'some default text' . (!empty($title) ? " &bull; ${title}" : null);
$this->set('title_for_layout', $title);
}

And, inside any action:

$this->_setPageTitle('whatever');

Of course, it'd be simple enough to replace the default text with
whatever is passed in, if you'd rather that.

For the nav, there are probably dozens of ways to do it, including on
the client side. Here's one example that I've used a couple of times,
both Cake and non-Cake:

<div id="nav">
<ul>
<?php
$links = array(
'news' => array(
'text' => 'news',
'route' => array('controller' => 'news', 'action' => 'index', 'admin' => 0),
'attr' => array('title' => 'news')
),
'catalog' => array(
'text' => 'catalog',
'route' => array('controller' => 'parts', 'action' => 'index', 'admin' => 0),
'attr' => array('title' => 'parts catalog')
),
'contact' => array(
'text' => 'contact',
'route' => array('controller' => 'pages', 'action' => 'display',
'slug' => 'contact', 'admin' => 0),
'attr' => array('title' => 'contact us')
)
);

foreach($links as $key => $link)
{
if ($this->here == "/${key}")
{
?>
<li id="<?= "nav_${key}" ?>" class="Selected"><?= $link['text'] ?></li>
<?php
}
else
{
?>
<li id="<?= "nav_${key}" ?>"><?= $html->link($link['text'],
$link['route'], $link['attr']) ?></li>
<?php
}
}
?>
</ul>
</div>

This is only suitable, of course, if your nav isn't likely to change
too much. You could dispense with the 'text' entry and simply use the
array key, except that the link text would need to be a single word.
Either that, or use an underscore, eg. 'my_key', and use
Inflector::humanize() for the link text.

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: