简体   繁体   English

$ this-> set('title','Title Name');不在CakePHP 3.x中工作

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

Basically in default.ctp I have this for my title: 基本上在default.ctp我有我的标题:

<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) 但它什么也没做,它仍然显示控制器名称(作业,控制器全名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) ? 那么$ this-> set('title',$ title)有什么问题?

fetch() returns the contents of a block not a variable. fetch()返回块的内容而不是变量。 Using set() in your Controller is setting a variable that can be output in your View templates by echoing the variable:- 在Controller中使用set()设置一个变量,可以通过回显变量在View模板中输出: -

<?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. 如果要使用fetch() ,则需要将其与View模板中的assign()结合使用以定义块。 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. 在CakePHP 3中,我们的想法是通过在视图中将页面标题与页面呈现相关联来设置页面标题。 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). 这与最初在CakePHP 2中处理的方式不同,在CakePHP 2中,您在控制器中定义了title_for_layout ,然后在布局模板中回显$title_for_layout变量(这在以后的Cake 2版本中不推荐使用CakePHP 3方法)。

You can just set() the variable in your controller: 您可以在控制器中set()变量:

// 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 详情请访问: http//book.cakephp.org/3.0/en/views.html#setting-view-variables

Using assign() is different, which is why it works with fetch() . 使用assign()是不同的,这就是它与fetch()一起使用的原因。 assign() is used with View Blocks: http://book.cakephp.org/3.0/en/views.html#using-view-blocks assign()与View Blocks一起使用: http//book.cakephp.org/3.0/en/views.html#using-view-blocks

为了完成,我遇到了一个情况,其中一个格式错误的.js脚本,在<head></head>之间引用了未定义的变量,导致<title></title>标签被发布到DOM(在页面源中显示)但Chrome ,Firefox和(来自内存) MSIE 未能将标题的内容再次从内存传递到APP UI - iOS移动版未受影响。

If you want stick to your code, after setting "title" variable just simply write this: 如果你想坚持你的代码,在设置“title”变量后,只需写下:

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

I have done this, this way in default.ctp 我在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');?>

In CakePHP 3 layout template, make sure to set the title as below. 在CakePHP 3布局模板中,请确保将标题设置如下。

<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. 这是CakePHP将使用其内置View类处理页面标题(视图块)渲染方案的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM