$this->set('title', 'Title Name'); not working in CakePHP 3.x

帅比萌擦擦* 提交于 2019-12-04 00:42:29

问题


Basically in default.ctp I have this for my title:

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

And inside of the controller I have this line:

$this->set('title', 'Test-Title');

But it does nothing, it still displays controllers name(Jobs, controllers full name os JobsController.ctp)

But if I put this inside of my view file:

$this->assign('title', 'Test-Title');

It changes the title. So what is wrong with $this->set('title', $title) ?


回答1:


You can just set() the variable in your controller:

// View or Controller
$this->set('title', 'Test-title');

Then use it as a standard variable is in your layout or view:

<!-- Layout or View -->
<title>
    <?php echo $title; ?>
</title>

Details here: http://book.cakephp.org/3.0/en/views.html#setting-view-variables

Using assign() is different, which is why it works with fetch(). assign() is used with View Blocks: http://book.cakephp.org/3.0/en/views.html#using-view-blocks




回答2:


fetch() returns the contents of a block not a variable. Using set() in your Controller is setting a variable that can be output in your View templates by echoing the variable:-

<?php echo $title; ?>

If you want to use fetch() you need to use it in combination with assign() in the View templates to define the block. For example in your View template use:-

<?php $this->assign('title', $title); ?>

And then in the layout template:-

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

In CakePHP 3 the idea is to set the page title by assigning it in the View as it relates to the rendering of the page. This differs from how this was originally handled in CakePHP 2 where you'd define title_for_layout in your controller and then echo the $title_for_layout variable in the layout template (this was deprecated in favour of the CakePHP 3 approach in later versions of Cake 2).




回答3:


Just for completion, I came across a situation where a malformed .js script with undefined variables referenced between <head></head> resulted in the <title></title> tags being posted to DOM (showed in page source) but Chrome, Firefox and (from memory) MSIE all failed to deliver the content of the title to the APP UI, again from memory - iOS mobile was unaffected.




回答4:


If you want stick to your code, after setting "title" variable just simply write this:

    <?= __('Main Project Name') ?>
    <?php if( isset($title)) $this->assign('title', $title); ?>
    <?= ' - ' . $this->fetch('title') ?>



回答5:


I have done this, this way in default.ctp

<?php
    $cakeDescription = __d('cake_dev', 'Your Title');
?>

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

In my view file, I have done this.

<?php $this->assign('title', 'Your Title');?>



回答6:


In CakePHP 3 layout template, make sure to set the title as below.

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

Then in your view:

<?php $this->assign('title', 'Title Name'); ?>

This is the way CakePHP will use its built-in View classes for handling page title (view blocks) rendering scenarios.



来源:https://stackoverflow.com/questions/30171030/this-settitle-title-name-not-working-in-cakephp-3-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!