quickstart-create-layout.xml 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 19777 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="learning.quickstart.create-layout">
  5. <title>Ein Layout erstellen</title>
  6. <para>
  7. Wie man festgestellen kann waren die View Skripte im vorhergehenden Kapitel HTML Fragmente-
  8. und keine kompletten Seiten. Das ist so gewünscht; wir wollen das unsere Aktionen nur den
  9. Inhalt zurückgeben der für die Aktion selbst relevant ist, und nicht die Anwendung als
  10. ganzes.
  11. </para>
  12. <para>
  13. Jetzt müssen wir den erstellten Inhalt zu einer kompletten HTML Seite zusammenfügen. Wir
  14. wollen auch ein konsistentes Aussehen und Feeling für die Anwendung haben. Wir wollen ein
  15. globales Sitelayout verwenden um beide Arbeiten zu ermöglichen.
  16. </para>
  17. <para>
  18. Es gibt zwei Design Pattern die Zend Framework verwendet um Layouts zu implementieren:
  19. <ulink url="http://martinfowler.com/eaaCatalog/twoStepView.html">Two Step View</ulink> und
  20. <ulink
  21. url="http://java.sun.com/blueprints/corej2eepatterns/Patterns/CompositeView.html">Composite
  22. View</ulink>. <emphasis>Two Step View</emphasis> wird normalerweise mit dem <ulink
  23. url="http://www.martinfowler.com/eaaCatalog/transformView.html">Transform View</ulink>
  24. Pattern assoziiert; die grundsätzliche Idee besteht darin das die View der Anwendung eine
  25. Repräsentation erstellt die dann in die Master View für letzte Transformationen injiziert
  26. wird. Das <emphasis>Composite View</emphasis> Pattern arbeitet mit einer View die aus ein
  27. oder mehreren atomischen Anwendungs Views gemacht ist.
  28. </para>
  29. <para>
  30. Im Zend Framework kombiniert <link linkend="zend.layout">Zend_Layout</link> die Ideen hinter
  31. diesen Pattern. Statt dass jedes Action View Skript Site-weite Artefakte einfügen muss,
  32. können Sie sich einfach auf Ihre eigenen Beantwortungen konzentrieren.
  33. </para>
  34. <para>
  35. Natürlich benötigt man trotzdem Anwendungs-spezifische Informationen im Site-weiten View
  36. Skript. Glücklicherweise bietet Zend Framework eine Anzahl von View
  37. <emphasis>Platzhaltern</emphasis> die es erlauben solche Informationen von den Action View
  38. Skripten zu bekommen.
  39. </para>
  40. <para>
  41. To get started using Zend_Layout, first we need to inform our bootstrap to use the
  42. <classname>Layout</classname> resource. This can be done by adding the following line to
  43. your <filename>application/configs/application.ini</filename> file, within the
  44. <constant>production</constant> section:
  45. </para>
  46. <programlisting language="ini"><![CDATA[
  47. ; application/configs/application.ini
  48. ; Add to [production] section:
  49. resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
  50. ]]></programlisting>
  51. <para>
  52. The final INI file should look as follows:
  53. </para>
  54. <programlisting language="ini"><![CDATA[
  55. ; application/configs/application.ini
  56. [production]
  57. ; PHP settings we want to initialize
  58. phpSettings.display_startup_errors = 0
  59. phpSettings.display_errors = 0
  60. includePaths.library = APPLICATION_PATH "/../library"
  61. bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  62. bootstrap.class = "Bootstrap"
  63. resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  64. resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
  65. [staging : production]
  66. [testing : production]
  67. phpSettings.display_startup_errors = 1
  68. phpSettings.display_errors = 1
  69. [development : production]
  70. phpSettings.display_startup_errors = 1
  71. phpSettings.display_errors = 1
  72. ]]></programlisting>
  73. <para>
  74. This directive tells your application to look for layout view scripts in
  75. <filename>application/layouts/scripts</filename>. Create those directories now.
  76. </para>
  77. <para>
  78. We also want to ensure we have an XHTML DocType declaration for our application. To enable
  79. this, we need to add a resource to our bootstrap.
  80. </para>
  81. <para>
  82. The simplest way to add a bootstrap resource is to simply create a protected method
  83. beginning with the phrase <methodname>_init</methodname>. In this case, we want to
  84. initialize the doctype, so we'll create an <methodname>_initDoctype()</methodname> method
  85. within our bootstrap class:
  86. </para>
  87. <programlisting language="php"><![CDATA[
  88. // application/Bootstrap.php
  89. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  90. {
  91. protected function _initDoctype()
  92. {
  93. }
  94. }
  95. ]]></programlisting>
  96. <para>
  97. Within that method, we need to hint to the view to use the appropriate doctype. But where
  98. will the view object come from? The easy solution is to initialize the
  99. <classname>View</classname> resource; once we have, we can pull the view object from the
  100. bootstrap and use it.
  101. </para>
  102. <para>
  103. To initialize the view resource, add the following line to your
  104. <filename>application/configs/application.ini</filename> file, in the section marked
  105. <constant>production</constant>:
  106. </para>
  107. <programlisting language="ini"><![CDATA[
  108. ; application/configs/application.ini
  109. ; Add to [production] section:
  110. resources.view[] =
  111. ]]></programlisting>
  112. <para>
  113. This tells us to initialize the view with no options (the '[]' indicates that the "view" key
  114. is an array, and we pass nothing to it).
  115. </para>
  116. <para>
  117. Now that we have a view, let's flesh out our <methodname>_initDoctype()</methodname> method.
  118. In it, we will first ensure the <classname>View</classname> resource has run, fetch the view
  119. object, and then configure it:
  120. </para>
  121. <programlisting language="php"><![CDATA[
  122. // application/Bootstrap.php
  123. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  124. {
  125. protected function _initDoctype()
  126. {
  127. $this->bootstrap('view');
  128. $view = $this->getResource('view');
  129. $view->doctype('XHTML1_STRICT');
  130. }
  131. }
  132. ]]></programlisting>
  133. <para>
  134. Now that we've initialized <classname>Zend_Layout</classname> and set the Doctype, let's
  135. create our site-wide layout:
  136. </para>
  137. <programlisting language="php"><![CDATA[
  138. // application/layouts/scripts/layout.phtml
  139. echo $this->doctype() ?>
  140. <html xmlns="http://www.w3.org/1999/xhtml">
  141. <head>
  142. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  143. <title>Zend Framework Quickstart Application</title>
  144. <?php echo $this->headLink()->appendStylesheet('/css/global.css') ?>
  145. </head>
  146. <body>
  147. <div id="header" style="background-color: #EEEEEE; height: 30px;">
  148. <div id="header-logo" style="float: left">
  149. <b>ZF Quickstart Application</b>
  150. </div>
  151. <div id="header-navigation" style="float: right">
  152. <a href="<?php echo $this->url(
  153. array('controller'=>'guestbook'),
  154. 'default',
  155. true) ?>">Guestbook</a>
  156. </div>
  157. </div>
  158. <?php echo $this->layout()->content ?>
  159. </body>
  160. </html>
  161. ]]></programlisting>
  162. <para>
  163. We grab our application content using the <methodname>layout()</methodname> view helper, and
  164. accessing the "content" key. You may render to other response segments if you wish to, but
  165. in most cases, this is all that's necessary.
  166. </para>
  167. <para>
  168. Note also the use of the <methodname>headLink()</methodname> placeholder. This is an easy
  169. way to generate the HTML for &lt;link&gt; elements, as well as to keep track of them
  170. throughout your application. If you need to add additional CSS sheets to support a single
  171. action, you can do so, and be assured it will be present in the final rendered page.
  172. </para>
  173. <note>
  174. <title>Checkpoint</title>
  175. <para>
  176. Now go to "http://localhost" and check out the source. You should see your XHTML header,
  177. head, title, and body sections.
  178. </para>
  179. </note>
  180. </sect1>