现在的位置: 首页 > 综合 > 正文

ZendFramework入门2 – 使用布局

2012年03月26日 ⁄ 综合 ⁄ 共 6370字 ⁄ 字号 评论关闭

You may have noticed that the view scripts in the previous sections were HTML 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.

现在我们必须把生成的内容组装到一个完整的HTML页面中去,而且我们希望整个程序的外观和感觉保持一致,所以我们将采用全局站点布局来完成这两项工作。

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. The Composite View pattern deals with a view made of one or more atomic, application views.

ZF实现布局有两种设计模式:两步视图和组合视图。两步视图通常关联着转换视图模式,基本思想是在最后转换的时候你程序创建一个内容然后注射到主视图里面。

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.

在ZF中,Zend_Layout组合这两种模式的思想。与每个动作试图脚本都需要包含一个全局站点的部件不同,他们只简单的把重点放到自己职责上。

Occasionally, however, you may need application-specific information in your site-wide view script. Fortunately, Zend Framework provides a variety of view placeholders to allow you to provide such information from your action view scripts.

有时候你需要放特定应用程序信息到你的全站视图脚本中。幸运的是,zf为你的动作试图脚本提供各种视图占位符。

To get started using Zend_Layout, first we need to inform our bootstrap to use the Layout resource. This can be done using the zf enable layout command:

为了开始使用Zend_Layout,首相我们需要通知bootstrap使用布局资源,我们使用zf enable layout命令。

   1: % zf enable layout

   2:  

   3: Layouts have been enabled, and a default layout created at

   4:  

   5: application/layouts/scripts/layout.phtml

A layout entry has been added to the application config file.

布局已经被加入到应用程序配置文件中了。

As noted by the command, application/configs/application.ini is updated, and now contains the following within the production section:

如命令指示的那样,application/configs/application.ini被更新了,在production章节包含如下信息:

   1: ; application/configs/application.ini

   2: ; Add to [production] section:

   3: resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"

The final INI file should look as follows:

最终的ini文件如下所示:

   1: ; application/configs/application.ini

   2: [production]

   3: ; PHP settings we want to initialize

   4: phpSettings.display_startup_errors = 0

   5: phpSettings.display_errors = 0

   6: includePaths.library = APPLICATION_PATH "/../library"

   7: bootstrap.path = APPLICATION_PATH "/Bootstrap.php"

   8: bootstrap.class = "Bootstrap"

   9: appnamespace = "Application"

  10: resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"

  11: resources.frontController.params.displayExceptions = 0

  12: resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"

  13: [staging : production]

  14: [testing : production]

  15: phpSettings.display_startup_errors = 1

  16: phpSettings.display_errors = 1

  17: [development : production]

  18: phpSettings.display_startup_errors = 1

  19: phpSettings.display_errors = 1

This directive tells your application to look for layout view scripts in application/layouts/scripts. If you examine your directory tree, you'll see that this directory has been created for you now, with the file layout.phtml.

这个指令告诉你的应用程序从application/layouts/scripts需找布局视图脚本。如果你去查看目录树,你会发现这个目录已经被创建了,里面也有了layout.phtml文件。

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.

我们同样想确认我们的应用程序已经有了XHTML DocType声明。为了启用这个,我们需要加入一个资源到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:

最简单增加一个bootstrap资源的方法是创建一个受保护的以_init开头的方法。我们想初始化doctype,既然这样,我们在bootstrap类中就创建一个_initDoctype()方法。

   1: // application/Bootstrap.php

   2: class Bootstrap extends Zend_Application_Bootstrap_Bootstrap

   3: {

   4:  

   5:     protected function _initDoctype()

   6:     {

   7:  

   8:     }

   9: }

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 the View resource; once we have, we can pull the view object from the bootstrap and use it.

在这个方法中,我们需要提示视图使用适当的doctype。但是视图对象从何而来?简单的方法就是初始化视图资源;一旦我们定义了视图资源,我们就能从bootstrap中得到视图对象并使用它。

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的production章节加入如下内容:

   1: ; application/configs/application.ini

   2: ; Add to [production] section:

   3: resources.view[] =

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键是一个数组,我们这里没有传参而已)。

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:

既然我们有了视图了,我们就能实现_initDoctype()方法。在代码中,我们首先得确认视图资源是否已经运行,获取视图对象并进行配置。

   1: // application/Bootstrap.php

   2: class Bootstrap extends Zend_Application_Bootstrap_Bootstrap

   3: {

   4:  

   5:     protected function _initDoctype()

   6:     {

   7:         $this->bootstrap('view');

   8:         $view = $this->getResource('view');

   9:         $view->doctype('XHTML1_STRICT');

  10:     }

  11: }

Now that we've initialized Zend_Layout and set the Doctype, let's create our site-wide layout:

现在我们已经初始化了Zend_Layout并设置了Doctype,接下来我们来创建全站布局:

   1: <!-- application/layouts/scripts/layout.phtml -->

   2: <?php echo $this->doctype() ?>

   3: <html xmlns="http://www.w3.org/1999/xhtml">

   4: <head>

   5:     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

   6:     <title>Zend Framework Quickstart Application</title>

   7:     <?php echo $this->headLink()->appendStylesheet('/css/global.css') ?>

   8: </head>

   9:  

  10: <body>

  11:     <div id="header" style="background-color: #EEEEEE; height: 30px;">

  12:         <div id="header-logo" style="float: left">

  13:             <b>ZF Quickstart Application</b>

  14:         </div>

  15:         <div id="header-navigation" style="float: right">

  16:             <a href="<?php echo $this->url(

  17:                             array('controller'=>'guestbook'),

  18:                                     'default',

  19:                                     true) ?>">Guestbook</a>

  20:         </div>

  21:     </div>

  22:     <?php echo $this->layout()->content ?>

  23: </body>

  24: </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()视图助手,并访问其content属性。如果你愿意你还可以加入其他respone段,在通常情况下这已经是所有的。

Note also the use of the headLink() placeholder. This is an easy way to generate the HTML 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()占位符。这是一个简单的方法来生成HTML的<link>元素。如果你需要增加一个额外的css文件来支持单个动作,你就可以这样做,它就能解析到最终的页面上。

抱歉!评论已关闭.