2

I'm setting individual page titles in my controllers' actions using:

$this->set( 'title_for_layout', 'Some Title' );

What I want to achieve is append a site title string to all these titles before rendering, i.e. say, "Some Title :: Site Name".

The easiest way out would be to add this string manually to each point where the page title is set - but that's like the brute force way.

What I tried was to override the beforeRender() method of each controller and add this statement:

$this->set( 'title_for_layout', $this->title_for_layout . ' » ' . Configure::read( 'Site.title' ) );

I thought this would append the site's title to the page title for each action - but what I get instead is:

Notice (8): Undefined variable: SomeController::title_for_layout [APP\controllers\some_controller.php, line xx]

It seems like the title_for_layout (set in individual actions in the controller) hasn't been set yet - which is throwing up this error.

My question is, where & How can I append the site title globally to all page titles - if not in this way?

Thanks, m^e

miCRoSCoPiC_eaRthLinG
  • 2,910
  • 4
  • 40
  • 56

2 Answers2

1

use $this->pageTitle instead of $this->title_for_layout

Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
  • That's what I had tried at first but $this->pageTitle doesn't seem to work and I read in another post on Nabble that $this->pageTitle has been deprecated in Cake 1.3.x in favour of title_for_layout. Moreover, it produces the same Undefined var notice. – miCRoSCoPiC_eaRthLinG Nov 30 '10 at 10:00
  • aw, sorry, coming from cakephp1.2 =/ – Andreas Wong Nov 30 '10 at 10:02
  • for one, I don't think you can access $this->title_for_layout in the controller, as it never been set in the controller, but rather in the view (thus the $this->set('title_for_layout') ). – Andreas Wong Nov 30 '10 at 10:05
  • Another tip is, if you want to make it applicablein all controllers, instead of doing it in every controller, you can do it in app_controller.php beforeRender() method – Andreas Wong Nov 30 '10 at 10:06
  • Yup thanks. I'm playing around with app_controller.php now... But still got to figure out how to avoid having to set it in every single view and be able to do it from a single point :) – miCRoSCoPiC_eaRthLinG Nov 30 '10 at 10:13
  • put the appending inside app/views/layout/default.ctp – Andreas Wong Nov 30 '10 at 10:14
  • Aahh.. I managed to get it done through the view :) Still there must be some more elegant way of doing the same out there. Thanks for all the help. – miCRoSCoPiC_eaRthLinG Nov 30 '10 at 10:16
1

If you really want to append the same string to all your page titles, put it in the layout after $title_for_layout.

blivet
  • 21,593
  • 1
  • 17
  • 17