创建布局
Create A Layout
你可能已经注意到在先前的文章里view脚本是HTML文档一个不完全的片段。这是设计的需要;我们想要我们的action返回仅仅和action本身相关的内容,不是整个程序。You may have noticed that the view scripts in the previous sections wereHTML fragments- not complete pages. This is by design; we want our actions to return content only related to the action itself, not the application as a whole.
现在我们必须把生成的内容组合进一个完整HTML页面。我们也想有一个看起来一致的程序。我们将使用一个全局的网站布局来完成这个任务。
Now we must compose that generated content into a full HTML page. We'd also like to have a consistent look and feel for the application. We will use a global site layout to accomplish both of these tasks.
Zend framework用两种设计模式来实施布局:两步View和组合View。两步视图通常和移植视图模式关联;基本的思路是程序试图创建一个表示方法,它为了最后移植插入主视图。组合视图模式处理一系列原子的、程序的视图。
There are two design patterns that Zend Framework uses to implement layouts:» Two Step View and» Composite View. Two Step View is usually associated with the» Transform View pattern; the basic idea is that your application view creates a representation that is then injected into the master view for final transformation. TheComposite View pattern deals with a view made of one or more atomic, application views.
在Zend Framework里,Zend_layout综合了这些模式背后的思想。和每个action视图脚本需要包含站点范围的操作相反,他们可以简单的聚焦于他们自己的责任。
In Zend Framework, Zend_Layout combines the ideas behind these patterns. Instead of each action view script needing to include site-wide artifacts, they can simply focus on their own responsibilities.
然而,有时在你的网站范围内的脚本里需要一些程序详细信息。幸运的是,Zend Framework提供了许多视图placeholders,允许你从你的action视图脚本里提供提供这样的信息。
Occasionally, however, you may need application-specific information in your site-wide view script. Fortunately, Zend Framework provides a variety of viewplaceholders to allow you to provide such information from your action view scripts.
为了开始使用zend——layout,首先我们需要通知我们的bootstrap使用Laout资源。
这个可以用zf enable layout命令完成。
(译注:需要首先进入quickstart的目录之后运行)
To get started using Zend_Layout, first we need to inform our bootstrap to use theLayout resource. This can be done using thezf enable layout command:
-
% zf enable layout
-
Layouts have been enabled, and a default layout created at
-
application/layouts/scripts/layout.phtml
-
A layout entry has been added to the application config file.
现在你打开application/configs/application.ini文件,看见添加了这么一行
As noted by the command, application/configs/application.ini is updated, and now contains the following within theproduction section:
; application/configs/application.ini
; Add to [production] section:
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
最后的INI文件应该看起来像以下:
The final INI file should look as follows:
; application/configs/application.ini
[production]
; PHP settings we want to initialize
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
这个指令告诉程序去application/layouts/scripts这里找layout视图。假如你检查你的目录树,你会看见现在已经建好了,有一个文件layout.phtml。
This directive tells your application to look for layout view scripts inapplication/layouts/scripts. If you examine your directory tree, you'll see that this directory has been created for you now, with the filelayout.phtml.
我们也想要确保我们有一个XHTML Doctype声明。要使这个可以用,我们需要到我们把资源加入到bootstrap。
最简便的实现方法是创建一个protected方法,以_ini开头。
在这个例子里,我们想初始化doctype,所以我们将在bootstrap类里创建一个_iniDoctype()方法。
We also want to ensure we have an XHTML DocType declaration for our application. To enable this, we need to add a resource to our bootstrap.
The simplest way to add a bootstrap resource is to simply create a protected method beginning with the phrase_init. In this case, we want to initialize the doctype, so we'll create an_initDoctype() method within our bootstrap class:
// application/Bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDoctype()
{
}
}
在那个方法里,我们需要暗示视图去使用合适的doctype。但是view的对象来自那里呢?
简单的方式是初始话view资源;一旦我们有了,我们能从bootstrap里找到view对象,就可以使用它了。
Within that method, we need to hint to the view to use the appropriate doctype. But where will the view object come from? The easy solution is to initialize theView resource; once we have, we can pull the view object from the bootstrap and use it.
为了初始化view资源,加下面的一行到你的程序 application/configs/application.ini文件里,在这个标记为production的部分。
To initialize the view resource, add the following line to your application/configs/application.ini file, in the section marked production:
; application/configs/application.ini
; Add to [production] section:
resources.view[] =
这告诉我们初始化view("[]"表示view key是一个数组,我们什么也没有传递给它。
This tells us to initialize the view with no options (the '[]' indicates that the "view" key is an array, and we pass nothing to it).
既然我们有一个view,让我们充实我们的_iniDoctype()方法。在它里,我们将首先确保View资源已经运行,查找View对象,然后配置它:
Now that we have a view, let's flesh out our _initDoctype() method. In it, we will first ensure the View resource has run, fetch the view object, and then configure it:
// application/Bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDoctype()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
}
}
Now that we've initialized Zend_Layout and set the Doctype, let's create our site-wide layout:
既然我们已经初始化了Zend_Layout而且设置了Doctype,让我们创建我们的站点级layout:
<!-- application/layouts/scripts/layout.phtml -->
<?php echo $this->doctype() ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Zend Framework Quickstart Application</title>
<?php echo $this->headLink()->appendStylesheet('/css/global.css') ?>
</head>
<body>
<div id="header" style="background-color: #EEEEEE; height: 30px;">
<div id="header-logo" style="float: left">
<b>ZF Quickstart Application</b>
</div>
<div id="header-navigation" style="float: right">
<a href="<?php echo $this->url(
array('controller'=>'guestbook'),
'default',
true) ?>">Guestbook</a>
</div>
</div>
<?php echo $this->layout()->content ?>
</body>
</html>
We grab our application content using the layout() view helper, and accessing the "content" key. You may render to other response segments if you wish to, but in most cases, this is all that's necessary.
我们使用layout()view helper抓取我们的程序内容,容易获取“content”的key。你可以渲染别的响应片段,假如你希望,但是,多数例子里,这些已经够了。
Note also the use of the headLink() placeholder. This is an easy way to generate theHTML for <link> elements, as well as to keep track of them throughout your application. If you need to add additional CSS sheets to support a single action, you can do so, and be assured it will be present in the final rendered page.
也记住headLink()placeholder的使用。这个是生成HTML<link>容易的方法,也是保持整个程序的踪迹。假如你需要加入额外的CSS文件支持一个单独的action,你也能这样,确保它在最后能被假如到页面。
现在去“http:\\localhost“,你应该能看见XHTML文件的header、head、title和body部分。
Note: CheckpointNow go to "http:\\localhost" and check out the source. You should see your XHTML header, head, title, and body sections.