所以我在 layout.phtml 上设置了一些默认元标记,使用
$this->headTitle() and $this->headMeta()->appendName()
并在 layout.phtml 的标题处回显
我的问题是:如何更改视图文件中的那些默认元标记以替换它们?
我尝试使用:
$this->headMeta()->appendName() or setName()
它不会替换旧的默认元标记,而是创建一个全新的元标记。我该如何替换它们?
所以我在 layout.phtml 上设置了一些默认元标记,使用
$this->headTitle() and $this->headMeta()->appendName()
并在 layout.phtml 的标题处回显
我的问题是:如何更改视图文件中的那些默认元标记以替换它们?
我尝试使用:
$this->headMeta()->appendName() or setName()
它不会替换旧的默认元标记,而是创建一个全新的元标记。我该如何替换它们?
我刚刚测试了这个,setName()
应该可以工作:
<?php $this->headMeta()->setName('keywords', 'test'); ?>
<?php $this->headMeta()->setName('keywords', 'test'); ?>
结果是:
<meta name="keywords" content="another test" >
尽管:
<?php $this->headMeta()->setName('keywords', 'test'); ?>
<?php $this->headMeta()->appendName('keywords', 'another test'); ?>
结果是:
<meta name="keywords" content="test" >
<meta name="keywords" content="another test" >
I would recommend setting a view variable for the keywords. For example, in your bootstrap.php you could define a default keywords as follows:
protected function _initSetDefaultKeywords() {
$view = $this->bootstrap('view')->getResource('view');
$view->keywords = 'default keywords';
}
In layout.phtml you would have then:
<?php echo $this->headMeta()->appendName('keywords', $this->keywords); ?>
Finally, in your views (or actions) you could change the meta keywords simply by changing the keywords view variable:
// in an action
$this->view->keywords = 'new kewords';
//or in a view
<?php $this->keywords = 'new kewords'; ?>
这对我有用。在布局文件中,您必须确保回显元标记。它在阶段是空的,但您将标记元标记的输出位置。这种方法的唯一缺点是似乎没有办法拥有默认元标记,因此您必须在每个视图文件中添加元标记。
在布局文件中
<?php echo $this->headMeta(); ?>
在视图 .phtml 文件中
$this->headMeta("test description text", "description");
没有控制器/动作的关键字和描述生成
在引导程序中注册 2 插件
// 注册导航,$view->navigation()->setContainer( new Zend_Navigation( $navigationArray ) ); $controller = Zend_Controller_Front::getInstance(); $controller->registerPlugin(new App_Controller_Plugin_PrepareNavigation()); $controller->registerPlugin(new App_Controller_Plugin_SetMeta());
元插件可能如下所示:
公共函数 routeShutdown(Zend_Controller_Request_Abstract $request) { $view = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')->view; $activePage = $view->navigation()->findOneBy('active', true); $view->headTitle($activePage->title); $view->headMeta()->appendName('keywords', $activePage->keywords ); $view->headMeta()->appendName('description', $activePage->description ); $view->pageHeader = $activePage->pageHeader; }
navigationArray 将比:
'页面' => 数组( 数组('标签' => '介绍', '控制器' => '控制器', '动作' => '介绍', '路线' => '控制器动作', 'pageHeader' => 'h1 什么的', 'title' => '用作元标题' '关键字' => '元关键字' '描述' => '元描述',
比你可以简单的调用(从布局/视图)打印 $this->pageHeader;
怎么样:
$keywords = 'php,zend,framework';
$this->view->headMeta($keywords,'keywords','name',array(),'SET');
我在 Zend 框架中放置元标记、链接和样式的最佳实践中找到了这个对我有用的东西?