ITeye博客

Zend Framework 1.10.4手册(ZF的快速启动三)

yangyz1984 · 2010-07-19 16:25 · zend framework · 编程语言 · CSS · PHP · Zend · 脚本 · XHTML · Markdown

你可能注意到在前面一节提到视图脚本是 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() 方法:

 

view plaincopy to clipboardprint?

  1. // application/Bootstrap.php
  2. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  3. {
  4. protected function _initDoctype()
  5. {
  6. }
  7. }

 

在这个方法内,我们需要提示视图来使用合适的 doctype。但是视图对象从哪里来?简单的方案是,初始化视图(View)资源;一旦我们完成,我们可以从 bootstrap 中扒下视图对象然后使用它。

为了初始化视图资源,把以下代码添加到你的 application/configs/applicationn.ini 文件中,在 production 部分下:

; application/configs/application.ini

; Add to [production] section:
resources.view[] =

这将告诉我们初始化视图但是没有选项([] 提示 view 键是一个数组,我们什么都没有传递给它)。

现在我们有了一个视图,让我们更新我们的 _initDoctype() 方法。在它里面,我们首先确保视图资源已经在运行,捕获视图对象,然后配置它:

 

view plaincopy to clipboardprint?

  1. // application/Bootstrap.php
  2. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  3. {
  4. protected function _initDoctype()
  5. {
  6. $this->bootstrap('view');
  7. $view = $this->getResource('view');
  8. $view->doctype('XHTML1_STRICT');
  9. }
  10. }

 

现在我们已经实例化了 Zend_Layout 并且设置了 Doctype,让我们创建我们的全站模板:

 

view plaincopy to clipboardprint?

  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. <body>
  10. <div id="header" style="background-color: #EEEEEE; height: 30px;">
  11. <div id="header-logo" style="float: left">
  12. <b>ZF Quickstart Application</b>
  13. </div>
  14. <div id="header-navigation" style="float: right">
  15. <a href="<?php echo $this->url(
  16. array('controller'=>'guestbook'),
  17. 'default',
  18. true) ?>">Guestbook</a>
  19. </div>
  20. </div>
  21. <?php echo $this->layout()->content ?>
  22. </body>
  23. </html>

 

我们使用 layout() 视图帮助器捕获应用程序的内容,同时访问 content 键。如果你愿意的话,你可以呈现其它的片断,但在大多数情况下,这是所需的全部了。

同时注意 headLink() 占位符的使用。这是为生成 HTML   <link> 元素的简单方式,同时还可以在你的应用程序内跟踪它们。如果你需要增加额外的 CSS 样式表来支持一个单独的行为,你可以这样做,并确保它会在最后的呈现页面中出现。

注意,检查
现在打开 http://localhost 并检查源代码,你应该看见你的 XHTML 头部,头部,标题和正文部分。

 

 

view plaincopy to clipboardprint?

  1. <!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">
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Zend Framework Quickstart Application</title>
  5. <link href="/css/global.css" media="screen" rel="stylesheet" type="text/css" /></head>
  6. <body>
  7. <div id="header" style="background-color: #EEEEEE; height: 30px;">
  8. <div id="header-logo" style="float: left">
  9. <b>ZF Quickstart Application</b>
  10. </div>
  11. <div id="header-navigation" style="float: right">
  12. <a href="/guestbook">Guestbook</a>
  13. </div>
  14. </div>
  15. <style>
  16. a:link,
  17. a:visited
  18. {
  19. color: #0398CA;
  20. }
  21. span#zf-name
  22. {
  23. color: #91BE3F;
  24. }
  25. div#welcome
  26. {
  27. color: #FFFFFF;
  28. background-image: url(http://framework.zend.com/images/bkg_header.jpg);
  29. width:  600px;
  30. height: 400px;
  31. border: 2px solid #444444;
  32. overflow: hidden;
  33. }
  34. div#more-information
  35. {
  36. background-image: url(http://framework.zend.com/images/bkg_body-bottom.gif);
  37. height: 100%;
  38. }
  39. </style>
  40. <center>
  41. <div id="welcome">
  42. <br />
  43. <h1>Welcome to the <span id="zf-name">Zend Framework!</span></h1>
  44. <h3>This is your project's main page</h3><br /><br />
  45. <h4><a href="zf.html">click here</a></h4>
  46. <div id="more-information">
  47. <br />
  48. <img src="http://framework.zend.com/images/PoweredBy_ZF_4LightBG.png" /><br /><br />
  49. Helpful Links: <br />
  50. <a href="http://framework.zend.com/">Zend Framework Website</a> |
  51. <a href="http://framework.zend.com/manual/en/">Zend Framework Manual</a>
  52. </div>
  53. </div>
  54. </center>
  55. </body>
  56. </html>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。