Zend Framework 1.10.4手册(ZF的快速启动三)
你可能注意到在前面一节提到视图脚本是 HTML 片断——不是完整的网页。这是出于设计的需要;我们希望我们的行为返回的内容只和行为本身有关,而不是整个应用程序。
现在我们必须把得到的内容合成到一个完整的 HTML 网页。我们也想为应用程序创建一个统一的外表和风格。我们将使用一个全站模板来完成这些任务。
Zend Framework 应用模板有两种设计模式:Two Step View 和 Composite View。Two Step View 通常和 Transform View 有关,基本的思想是,你的应用程序的视图创建一个表现,然后为了最后的转换(for final transformation)注入到主模板中。而 Composite View 模式则处理一个由一个或者更多个的原子,应用程序视图组合而成的视图。
在 Zend Framework 中,Zend_Layout 组合了这些模式的思想。不同于每一个行为视图需要包括全站的东西,他们可以简单的关注它们自己的责任。
偶尔的,然而,你可能需要应用程序方面的信息出现在你的全站视图脚本中,幸运的是,Zend Framework 提供了大量的视图占位符(placeholder)来允许你从你的行为视图脚本提供这样的信息(应用程序方面的信息 application-specific information)。
为了开始使用 Zend_Layout,首先我们需要通知我们的 bootstrap 来使用 Layout 资源(Bootstrap 类定义了什么资源和组件将要初始化。)。这可以通过使用 zf enable layout 命令来完成:
% 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 被更新了,现在 production 部分包括了以下代码:
; application/configs/application.ini
; Add to [production] section:
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
最后的 INI 文件应该看起来是这样的:
; 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.phtml 这个文件。
我们也想确保针对我们的应用程序,我们有一个 XHTML DocType 声明。为了做到这个,我们需要添加一个资源到我们的 bootstrap。
添加一个资源到 bootstrap 的最简单的方法是创建一个以 _init 短语开头的私有方法。在这个例子中,我们想要初始化 doctype,所以我们在我们的 bootstrap 类中创建一个 _initDoctype() 方法:
- // application/Bootstrap.php
- class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
- {
- protected function _initDoctype()
- {
- }
- }
// application/Bootstrap.php class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { protected function _initDoctype() { } }
在这个方法内,我们需要提示视图来使用合适的 doctype。但是视图对象从哪里来?简单的方案是,初始化视图(View)资源;一旦我们完成,我们可以从 bootstrap 中扒下视图对象然后使用它。
为了初始化视图资源,把以下代码添加到你的 application/configs/applicationn.ini 文件中,在 production 部分下:
; application/configs/application.ini
; Add to [production] section:
resources.view[] =
这将告诉我们初始化视图但是没有选项([] 提示 view 键是一个数组,我们什么都没有传递给它)。
现在我们有了一个视图,让我们更新我们的 _initDoctype() 方法。在它里面,我们首先确保视图资源已经在运行,捕获视图对象,然后配置它:
- // application/Bootstrap.php
- class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
- {
- protected function _initDoctype()
- {
- $this->bootstrap('view');
- $view = $this->getResource('view');
- $view->doctype('XHTML1_STRICT');
- }
- }
// application/Bootstrap.php class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { protected function _initDoctype() { $this->bootstrap('view'); $view = $this->getResource('view'); $view->doctype('XHTML1_STRICT'); } }
现在我们已经实例化了 Zend_Layout 并且设置了 Doctype,让我们创建我们的全站模板:
- <!-- 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>
<!-- 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>
我们使用 layout() 视图帮助器捕获应用程序的内容,同时访问 content 键。如果你愿意的话,你可以呈现其它的片断,但在大多数情况下,这是所需的全部了。
同时注意 headLink() 占位符的使用。这是为生成 HTML <link> 元素的简单方式,同时还可以在你的应用程序内跟踪它们。如果你需要增加额外的 CSS 样式表来支持一个单独的行为,你可以这样做,并确保它会在最后的呈现页面中出现。
注意,检查
现在打开 http://localhost 并检查源代码,你应该看见你的 XHTML 头部,头部,标题和正文部分。
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><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>
- <link href="/css/global.css" media="screen" rel="stylesheet" type="text/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="/guestbook">Guestbook</a>
- </div>
- </div>
- <style>
- a:link,
- a:visited
- {
- color: #0398CA;
- }
- span#zf-name
- {
- color: #91BE3F;
- }
- div#welcome
- {
- color: #FFFFFF;
- background-image: url(http://framework.zend.com/images/bkg_header.jpg);
- width: 600px;
- height: 400px;
- border: 2px solid #444444;
- overflow: hidden;
- }
- div#more-information
- {
- background-image: url(http://framework.zend.com/images/bkg_body-bottom.gif);
- height: 100%;
- }
- </style>
- <center>
- <div id="welcome">
- <br />
- <h1>Welcome to the <span id="zf-name">Zend Framework!</span></h1>
- <h3>This is your project's main page</h3><br /><br />
- <h4><a href="zf.html">click here</a></h4>
- <div id="more-information">
- <br />
- <img src="http://framework.zend.com/images/PoweredBy_ZF_4LightBG.png" /><br /><br />
- Helpful Links: <br />
- <a href="http://framework.zend.com/">Zend Framework Website</a> |
- <a href="http://framework.zend.com/manual/en/">Zend Framework Manual</a>
- </div>
- </div>
- </center>
- </body>
- </html>