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

后端 未结 6 1133
温柔的废话
温柔的废话 2021-01-11 19:42

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


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

And inside of the

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-11 19:56

    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:-

    
    

    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:-

    assign('title', $title); ?>
    

    And then in the layout template:-

    <?php echo $this->fetch('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).

提交回复
热议问题