How can I set title_for_layout to the default PagesController?

I cannot set title_for_layout in PagesController, which comes by default with CakePHP 1.3.

I use the following code in a display function:

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

What am I doing wrong?

+4
source share
7 answers

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

UPDATE

Unfortunately, as noted in the comments, this solution is 1.2. 1.3 features (after some research) include:

  • Ensuring $title_for_layout reflected in the layout
  • Placing the code $this->set() in the view, not in the controller
+7
source

If you want to emulate 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 can use pageTitle, as in 1.2.

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

The beforeRender () method is called after your controllers have finished processing, but before rendering the layout, which allows you to set variables for the layout.

+3
source

In the action method, try the following:

 function index() { $this->pageTitle= 'Your Title'; } 
+2
source

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

+1
source

You can use:

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

and in ctp:

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

Works in CakePHP 3.0

+1
source

For CakePHP 3.x,

you can follow the tips here

Essentially

Inside UsersController.php :

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

Inside src/Template/Layouts/default.ctp

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

records:

 if (isset($title)) { $this->assign('title', $title); } 
+1
source

Use beforeRender() instead. In the AppController add the following:

 class AppController extends Controller { var $content; function beforeRender() { $this->set('content',$this->content); } } 

And in the controller, you can simply do:

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

It will be a trick.

0
source

Source: https://habr.com/ru/post/1306782/


All Articles