5

I cannot set title_for_layout in the PagesController that comes by default with CakePHP 1.3.

I am using the following code in the display function:

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

What am I doing wrong?

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
ion
  • 1,033
  • 2
  • 16
  • 46
  • Update: The **`$title_for_layout`** is deprecated as of 2.5 [Link](http://book.cakephp.org/2.0/en/views.html#layouts) – Vy Do Nov 29 '14 at 09:27

7 Answers7

7

In your controller, the corresponding value is $this->pageTitle.

UPDATE

Oops, as noted in the comments, this is the 1.2 solution. 1.3 possibilities (after doing some research) include:

  1. Ensuring that $title_for_layout is being echoed in the layout
  2. Placing the $this->set() code in the view rather than in the controller
Rob Wilkerson
  • 40,476
  • 42
  • 137
  • 192
  • 1
    As an addition: you can also set this value in the corresponding View which would be even more MVC – harpax Apr 13 '10 at 12:53
  • 1
    Sorry guys but $this->PageTitle is for cakephp 1.2 and not for 1.3 in my knowledge. In any case when used in cakephp 1.2 produces an error. – ion Apr 13 '10 at 14:13
  • Apologies. I missed the 1.3 specification in your question if that wasn't part of an edit. It does look like that's gone away in 1.3 (http://book.cakephp.org/view/1561/Migrating-from-CakePHP-1-2-to-1-3). I haven't used 1.3 yet, but a read of the docs indicates that what you're doing should work if the rest of the context is correct. – Rob Wilkerson Apr 13 '10 at 15:10
  • At the risk of stating the obvious, are you echoing the `$title_for_layout` variable in your actual layout? e.g. `<?php echo $title_for_layout; ?>`? – Rob Wilkerson Apr 13 '10 at 19:21
  • Looks like the solution was to put the $this->set() code in the view and not in the controller. – ion Apr 16 '10 at 13:28
3

If you'd like to mimic the behavior of cake 1.2, you can do the following:

In your app_controller, create the following method:

in app/app_controller.php (you may need to create this file if you haven't already)

public $pageTitle;

public function beforeRender() {
    $this->set('title_for_layout', $this->pageTitle);
}

Then in any of your action methods, you may then use the pageTitle as you would in 1.2.

public function index() {
    $this->pageTitle = 'Some Title';
}

The beforeRender() method will be called after your controllers have finished processing, but prior to the layout being rendered, thus allowing you to set variables for the layout.

aminal
  • 105
  • 6
2

In the action method, try this:

function index()
{
    $this->pageTitle= 'Your Title';
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Chirag
  • 313
  • 1
  • 9
1

You can use :

$this->assign('title', 'Some title');

and in ctp :

<title><?= $this->fetch('title'); ?></title>

It work in CakePHP 3.0

Obi
  • 81
  • 1
1

For CakePHP 3.x,

you can follow the advice here

Essentially,

Inside UsersController.php:

$this->set('title', 'Login');

Inside src/Template/Layouts/default.ctp

above the $this->fetch('title');

write:

if (isset($title)) {
    $this->assign('title', $title); 
}
Community
  • 1
  • 1
Kim Stacks
  • 10,202
  • 35
  • 151
  • 282
1

Just thought I'd add to any new people finding this solution, you can do $this->set('title', $title); in CakePHP 1.3 inside the controller and the title will be rendered automatically.

Calvin
  • 11
  • 1
0

Use beforeRender() instead. Put the following in AppController:

class AppController extends Controller {
    var $content;

    function beforeRender() {
        $this->set('content',$this->content);
    }            
}

And in the controller, you can just do:

function index() {
    $this->content['title'] = '123'; }

That'll do the trick.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
windmaomao
  • 7,120
  • 2
  • 32
  • 36