简体   繁体   English

Zend Framework:headTitle() - > append()问题

[英]Zend Framework: headTitle()->append() issue

Has anyone run into this problem... 有没有人遇到这个问题......

In my layout.phtml I have: 在我的layout.phtml中,我有:

<head>
    <?= $this->headTitle('Control Application - ') ?>
</head>

then in index.phtml I have: 然后在index.phtml我有:

<? $this->headTitle()->append('Client List'); ?>

I expect that, when I go to my index action, the title should be 'Control Application - Client List' but instead I have 'Client ListControl Application - ' 我希望,当我进行索引操作时,标题应该是“控制应用程序 - 客户端列表”,而是我有“客户端列表控制应用程序 - ”

What is going on? 到底是怎么回事? How can I fix this? 我怎样才能解决这个问题?

Default behaviour of the headTitle() is to append to the stack. headTitle()的默认行为是附加到堆栈。 Before calling headTitle() in layout.phtml, your stack is: 在layout.phtml中调用headTitle()之前,您的堆栈是:

Clientlist 客户端列表

Then, you call headTitle with the first argument and no second argument ( which makes it default to APPEND ), resulting in the following stack: 然后,使用第一个参数调用headTitle而没有第二个参数( 这使其默认为APPEND ),从而产生以下堆栈:

ClientListControl Application - ClientListControl应用程序 -

The solution, in layout.phtml: 解决方案,在layout.phtml中:

<?php 
    $this->headTitle()->prepend('Control Application -');
    echo $this->headTitle();
?>

Additionally, you can use the setPrefix method in your layout as such: 此外,您可以在布局中使用setPrefix方法:

<head>
    <?= $this->headTitle()->setPrefix('Control Application') ?>
</head>

And in your controllers/actions/etc use the standard append/prepend: 在您的controllers / actions / etc中使用标准append / prepend:

<?php
$this->headTitle()->setSeparator(' - ');
$this->headTitle()->append('Client List');
?>

I don't actually use headTitle, but do use ZF, and I had a quick look on the mailing list, this might solve the problem: 我实际上并没有使用headTitle,而是使用ZF,我快速查看了邮件列表,这可能会解决问题:

<head>
    <?= $this->headTitle('Control Application') ?>
</head>

Then: 然后:

<?php
$this->headTitle()->setSeparator(' - ');
$this->headTitle()->prepend('Client List');
?>

This happens because the layout is the last script to be executed. 发生这种情况是因为布局是要执行的最后一个脚本。 So you actually do the append BEFORE the set of the title, so that there's nothing to append to yet. 所以你实际上在标题集之前进行追加,这样就没有什么可以追加了。 Set the main title (Control Application) in a Controller. 在Controller中设置主标题(Control Application)。 For example I always do it in the predispatch action of a initPlugin so that it is execute before any other Controller Action, and I can append or prepend at will. 例如,我总是在initPlugin的predispatch操作中执行它,以便它在任何其他Controller Action之前执行,并且我可以随意附加或前置。

To use such a plugin just define a new Class extending Zend_Controller_Plugin_Abstract and define a function preDispatch( Zend_Controller_Request_Abstract $request) where you can put all your common-to-the-whole-site code, and to register the plugin just put it into the controllerFront of your bootstrap: $controller->registerPlugin(new InitPlugin()); 要使用这样的插件,只需定义一个新的类扩展Zend_Controller_Plugin_Abstract并定义一个函数preDispatch( Zend_Controller_Request_Abstract $ request),您可以在其中放置所有常见的整个站点代码,并注册插件只需将其放入controllerFront你的bootstrap:$ controller-> registerPlugin(new InitPlugin());

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

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