Setting the page title

Published on and tagged with cakephp  controller  view

If you want to set the title of a page, you have three different ways to accomplish that.

The first two approaches use the magic variable “title_for_layout” to set the title in the layout:

<title>
    <?php echo $title_for_layout;?>
</title>

You can set the value for this magic variable either in the controller:

public $pageTitle = 'The title';  // same title for all actions

or

public function test() {
    $this->pageTitle = 'The title';
    ...
}

or in the view file:

<?php $this->pageTitle = 'The title';?>
...

The third, and last, approach uses a custom variable in the layout instead of the magic variable:

<title>
    <?php echo $pageTitle;?>
</title>

Its value is then set in the controller, in the same way you put other data to the view:

public function test() {
    ...
    $this->set('pageTitle', 'The title');
}

Now, which approach should you use? I think it is a matter of preference. From a conceptual point of view I prefer the third approach as it doesn’t use any magic, but in practice I usually use the second (maybe this will change after this article *g*). The first approach I consider as a bad practice, because a controller doesn’t represent a page and hence it shouldn’t have a pageTitle property.

11 comments baked

  • sosa

    I set it in each view like this

    pageTitle=”A SEO-friendly title for this view” ?>

    and I kill two birds with one stone :D

  • John Reeves

    I’ve always been under the impression that the tag was part of the “View” logic and therefore I set it at the top of the .ctp file before any other HTML.

  • Bret Kuhns

    I like Sosa’s idea and never even thought you’d be able to do that. I assumed since the head/title portion of the layout comes before the page view, setting the title in the view wouldn’t work. I guess I should’ve just tried it to see what happens. Thanks for pointing this out guys :)

  • dr. Hannibal Lecter

    I do the same as John Reeves, as the page title seems a part of the view. I do combine it though, in the layout, with the site name variable in my configuration.

  • Wouter de Boer

    Another method $this->set(‘title’, ‘nice title’); works with $title_for_layout; but confusing if you’ve a (blog??)title in your content.

  • Anonymous

    3 distinct ways to set titles is stupid. The special handling of ‘title’ in View::set() should go away — why not pull pageTitle out of viewVars?

    blech.

  • cakebaker

    @all: Thanks for your comments!

    @Wouter: Thanks for your hint, I wasn’t aware that “title” is treated differently.

    @Anonymous: I agree with you, it would be better if there would be only one way to set a title. And instead of the special handling of “title” it would be more logical to use “title_for_layout”, so that you have to use the same name in the controller and the layout.

  • Anupom

    Well, I was really confused and thought it’s a bug when I found the fact that setting view variable as ‘title’ actually changes the title of the page. Like, as Wouter mentioned,
    $this->set(’title’, ‘nice title’); works with $title_for_layout;

    I think it’s unnecessary. There are already few different ways of setting the page title. Why do we need this then? :'(

  • cakebaker

    @Anupom: Yes, I agree with you, it’s not obvious that there is a special handling for “title”. See also my answer to Anonymous.

  • Matti Putkonen

    I would say setting the title in view is the right thing to do. The title of the view does not concern the controller.

  • cakebaker

    @Matti: Yes, in most cases it is the right thing to set the title in the view.

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License