Rob 的回答是正确的,尽管您可能需要更多信息,因为您似乎正在努力使这变得复杂。
ZF 在用作 MVC 时有放置布局和使用它们的默认值的地方。
如果您使用 Zend_Tool 命令行界面,请从 commmand: 开始,zf enable layout
该工具会将默认目录和默认layout.phtml添加到您的项目中。
在application.ini中,它将添加以下行:
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
并在该路径上添加layout.phtml
文件
resources.layout.layout = master
您可以想到使用此文件的多种方法,但这里是我如何使用它的示例。我喜欢在我的application.ini
文件
中设置我的项目默认值,因此如果我需要更改任何内容,这很容易。
View Settings
;*************
resources.view[]=
resources.view.charset = "UTF-8"
resources.view.encoding = "UTF-8"
resources.view.doctype = "HTML5"
resources.view.language = "en"
resources.view.contentType = "text/html; charset=UTF-8"
然后在我的引导程序中,我设置了我想要使用的视图,我在这里进行设置,以便如果我有多个布局(我通常这样做),很容易在一个地方更改 css 或 js 文件。
protected function _initView() {
//Initialize view
$view = new Zend_View();
$view->addHelperPath('/../library/Application/View/Helper');
$view->doctype(Zend_Registry::get('config')->resources->view->doctype);
$view->headTitle('Our Home');
$view->headMeta()->appendHttpEquiv('Content-Type', Zend_Registry::get(
'config')->resources->view->contentType);
$view->headLink()->setStylesheet('/css/normalize.css');
$view->headLink()->appendStylesheet('/css/blueprint/src/liquid.css');
$view->headLink()->appendStylesheet('/css/blueprint/src/typography.css');
$view->headLink()->appendStylesheet(
'/javascript/mediaelement/build/mediaelementplayer.css');
$view->headLink()->appendStylesheet('/css/main.css');
$view->headLink()->appendStylesheet('/css/nav.css');
$view->headLink()->appendStylesheet('/css/table.css');
//add javascript files
$view->headScript()->setFile('/javascript/mediaelement/build/jquery.js');
$view->headScript()->appendFile('/javascript/modernizr.js');
//add it to the view renderer
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer');
$viewRenderer->setView($view);
//Return it, so that it can be stored by the bootstrap
return $view;
}
请注意所有这些 headLink()、headScript() 和 docType() 条目,这些是为占位符设置数据的位置,将在布局中使用。
现在布局,来自其他基于动作的脚本的实际内容通常由占位符呈现$this->layout()->content
<?php
echo $this->doctype() . "\n";//placeholder
?>
<html>
<head>
<title></title>
<?php echo $this->headMeta() . "\n" ?><!-- displays all meta data passed -->
<?php echo $this->headLink() . "\n" ?><!-- displays all links passed -->
<?php echo $this->headscript(). "\n"?><!-- displays all scripts passed -->
</head>
<body>
<section class="container">
<header class="block">
<hgroup id="header" class ="column span-24">
<h1>Our Home</h1>
</hgroup>
<nav>
<div id="nav" class="column span-24">
<?php echo $this->layout()->nav ?> <!-- Custom Placeholder -->
</div>
</nav>
</header>
<section class="block">
<div id="main" class="column span-18 border">
<div id="flash">
<?php
//flash messenger display location
if (count($this->messages) > 0) {
printf("<h3 id='flash'>%s</h3>", $this->messages[0]);
}
?>
</div>
<?php echo $this->layout()->content; ?><!-- Default placeholder, where views are rendered -->
</div>
<aside id="sidebar" class="column span-4 last">
<?php echo $this->layout()->search ?><!-- Custom placeholder -->
<div id="subNav">
<?php echo $this->layout()->subNav ?> <!-- Custom placeholder -->
</div>
<div id="adminMenu">
<h4>Administration Links</h4>
<?php echo $this->layout()->adminMenu ?> <!-- Custom placeholder -->
</div>
</aside>
</section>
<footer class="block">
<div id="footer" class="column span-24">
<p>Created with <a href="http://framework.zend.com/">Zend Framework. © </a></p>
</div>
</footer>
</section>
<?php echo $this->inlineScript() ?><!-- placeholder -->
</body>
</html>
希望这可以帮助!
[编辑]
还有一件事,这似乎总是下一个问题。“如何更改我的控制器/操作中的默认布局?”
要从控制器更改布局,您通常会使用 preDispatch() 方法进行更改,并将新布局的名称传递给布局助手。
$this->_helper->layout->setLayout('myOtherLayout');
这样做会改变控制器中每个动作的布局。为了更有选择性,您可以使用条件,例如:
if ($this->getRequest()->getActionName() == 'player') {
$this->_helper->layout->setLayout('player');//reset layout
//add 2 new headscripts
$this->view->headScript()->appendFile(
'http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js'
);
$this->view->headScript()->appendFile(
'/javascript/mediaplayer/jwplayer.js'
);
}