view-placeholders-standard.xml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 19777 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="learning.view.placeholders.standard">
  5. <title>Standard Placeholders</title>
  6. <para>
  7. In the <link linkend="learning.view.placeholders.basics">previous section</link>, we learned
  8. about the <methodname>placeholder()</methodname> view helper, and how it can be used to
  9. aggregate custom content. In this section, we'll look at some of the concrete placeholders
  10. shipped with Zend Framework, and how you can use them to your advantage when creating
  11. complex composite layouts.
  12. </para>
  13. <para>
  14. Most of the shipped placeholders are for generating content for the
  15. <code>&lt;head&gt;</code> section of your layout content -- an area you typically cannot
  16. manipulate directly via your application view scripts, but one you may want to influence. As
  17. examples: you may want your title to contain certain content on every page, but specific
  18. content based on the controller and/or action; you may want to specify
  19. <acronym>CSS</acronym> files to load based on what section of the application you're in; you
  20. may need specific JavaScript scripts loaded at different times; or you may want to set the
  21. <acronym>DocType</acronym> declaration.
  22. </para>
  23. <para>
  24. Zend Framework ships with placeholder implementations for each of these situations, and
  25. several more.
  26. </para>
  27. <sect2 id="learning.view.placeholders.standard.doctype">
  28. <title>Setting the DocType</title>
  29. <para>
  30. <acronym>DocType</acronym> declarations are troublesome to memorize, and often essential
  31. to include in your document to ensure the browser properly renders your content. The
  32. <methodname>doctype()</methodname> view helper allows you to use simple string mnemonics
  33. to specify the desired <acronym>DocType</acronym>; additionally, other helpers will
  34. query the <methodname>doctype()</methodname> helper to ensure the output generated
  35. conforms with the requested <acronym>DocType</acronym>.
  36. </para>
  37. <para>
  38. As an example, if you want to use the <acronym>XHTML1</acronym> Strict
  39. <acronym>DTD</acronym>, you can simply specify:
  40. </para>
  41. <programlisting language="php"><![CDATA[
  42. $this->doctype('XHTML1_STRICT');
  43. ]]></programlisting>
  44. <para>
  45. Among the other available mnemonics, you'll find these common types:
  46. </para>
  47. <variablelist>
  48. <varlistentry>
  49. <term>XHTML1_STRICT</term>
  50. <listitem>
  51. <para>
  52. <acronym>XHTML</acronym> 1.0 Strict
  53. </para>
  54. </listitem>
  55. </varlistentry>
  56. <varlistentry>
  57. <term>XHTML1_TRANSITIONAL</term>
  58. <listitem>
  59. <para>
  60. <acronym>XHTML</acronym> 1.0 Transitional
  61. </para>
  62. </listitem>
  63. </varlistentry>
  64. <varlistentry>
  65. <term>HTML4_STRICT</term>
  66. <listitem>
  67. <para>
  68. <acronym>HTML</acronym> 4.01 Strict
  69. </para>
  70. </listitem>
  71. </varlistentry>
  72. <varlistentry>
  73. <term>HTML4_Loose</term>
  74. <listitem>
  75. <para>
  76. <acronym>HTML</acronym> 4.01 Loose
  77. </para>
  78. </listitem>
  79. </varlistentry>
  80. <varlistentry>
  81. <term>HTML5</term>
  82. <listitem>
  83. <para>
  84. <acronym>HTML</acronym> 5
  85. </para>
  86. </listitem>
  87. </varlistentry>
  88. </variablelist>
  89. <para>
  90. You can assign the type and render the declaration in a single call:
  91. </para>
  92. <programlisting language="php"><![CDATA[
  93. echo $this->doctype('XHTML1_STRICT');
  94. ]]></programlisting>
  95. <para>
  96. However, the better approach is to assign the type in your bootstrap, and then render it
  97. in your layout. Try adding the following to your bootstrap class:
  98. </para>
  99. <programlisting language="php"><![CDATA[
  100. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  101. {
  102. protected function _initDocType()
  103. {
  104. $this->bootstrap('View');
  105. $view = $this->getResource('View');
  106. $view->doctype('XHTML1_STRICT');
  107. }
  108. }
  109. ]]></programlisting>
  110. <para>
  111. Then, in your layout script, simply <function>echo</function> the helper at the
  112. top of the file:
  113. </para>
  114. <programlisting language="php"><![CDATA[
  115. <?php echo $this->doctype() ?>
  116. <html>
  117. <!-- ... -->
  118. ]]></programlisting>
  119. <para>
  120. This will ensure that your DocType-aware view helpers render the appropriate markup,
  121. ensure that the type is set well before the layout is rendered, and provide a single
  122. location to change the DocType.
  123. </para>
  124. </sect2>
  125. <sect2 id="learning.view.placeholders.standard.head-title">
  126. <title>Specifying the Page Title</title>
  127. <para>
  128. Often, a site will include the site or business name as part of the page title, and
  129. then add additional information based on the location within the site. As an example,
  130. the zend.com website includes the string "Zend.com" on all pages, and the prepends
  131. information based on the page: "Zend Server - Zend.com". Within Zend Framework, the
  132. <methodname>headTitle()</methodname> view helper can help simplify this task.
  133. </para>
  134. <para>
  135. At its simplest, the <methodname>headTitle()</methodname> helper allows you to aggregate
  136. content for the <code>&lt;title&gt;</code> tag; when you echo it, it then assembles it
  137. based on the order in which segments are added. You can control the order using
  138. <methodname>prepend()</methodname> and <methodname>append()</methodname>, and provide a
  139. separator to use between segments using the <methodname>setSeparator()</methodname>
  140. method.
  141. </para>
  142. <para>
  143. Typically, you should specify any segments common to all pages in your bootstrap,
  144. similar to how we define the doctype. In this case, we'll define a
  145. <methodname>_initPlaceholders()</methodname> method for operating on all the various
  146. placeholders, and specify an initial title as well as a separator.
  147. </para>
  148. <programlisting language="php"><![CDATA[
  149. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  150. {
  151. // ...
  152. protected function _initPlaceholders()
  153. {
  154. $this->bootstrap('View');
  155. $view = $this->getResource('View');
  156. $view->doctype('XHTML1_STRICT');
  157. // Set the initial title and separator:
  158. $view->headTitle('My Site')
  159. ->setSeparator(' :: ');
  160. }
  161. // ...
  162. }
  163. ]]></programlisting>
  164. <para>
  165. Within a view script, we might want to add another segment:
  166. </para>
  167. <programlisting language="php"><![CDATA[
  168. <?php $this->headTitle()->append('Some Page'); // place after other segments ?>
  169. <?php $this->headTitle()->prepend('Some Page'); // place before ?>
  170. ]]></programlisting>
  171. <para>
  172. In our layout, we will simply echo the <methodname>headTitle()</methodname> helper:
  173. </para>
  174. <programlisting language="php"><![CDATA[
  175. <?php echo $this->doctype() ?>
  176. <html>
  177. <?php echo $this->headTitle() ?>
  178. <!-- ... -->
  179. ]]></programlisting>
  180. <para>
  181. This will generate the following output:
  182. </para>
  183. <programlisting language="html"><![CDATA[
  184. <!-- If append() was used: -->
  185. <title>My Site :: Some Page</title>
  186. <!-- If prepend() was used: -->
  187. <title>Some Page :: My Site</title>
  188. ]]></programlisting>
  189. </sect2>
  190. <sect2 id="learning.view.placeholders.standard.head-link">
  191. <title>Specifying Stylesheets with HeadLink</title>
  192. <para>
  193. Good CSS developers will often create a general stylesheet for sitewide styles, and
  194. individual stylesheets for specific sections or pages of the website, and load these
  195. latter conditionally so as to decrease the amount of data needing to be transferred on
  196. each request. The <methodname>headLink()</methodname> placeholder makes such conditional
  197. aggregation of stylesheets trivial within your application.
  198. </para>
  199. <para>
  200. To accomplish this, <methodname>headLink()</methodname> defines a number of "virtual"
  201. methods (via overloading) to make the process trivial. The ones we will be concerned
  202. with are <methodname>appendStylesheet()</methodname> and
  203. <methodname>prependStylesheet()</methodname>. Each takes up to four arguments,
  204. <varname>$href</varname> (the relative path to the stylesheet),
  205. <varname>$media</varname> (the MIME type, which defaults to "text/css"),
  206. <varname>$conditionalStylesheet</varname> (which can be used to specify a "condition"
  207. under which the stylesheet will be evaluated), and <varname>$extras</varname> (an
  208. associative array of key/value pairs, commonly used to specify a key for "media"). In
  209. most cases, you will only need to specify the first argument, the relative path to the
  210. stylesheet.
  211. </para>
  212. <para>
  213. In our example, we'll assume that all pages need to load the stylesheet located in
  214. "/styles/site.css" (relative to the document root); we'll specify this in our
  215. <methodname>_initPlaceholders()</methodname> bootstrap method.
  216. </para>
  217. <programlisting language="php"><![CDATA[
  218. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  219. {
  220. // ...
  221. protected function _initPlaceholders()
  222. {
  223. $this->bootstrap('View');
  224. $view = $this->getResource('View');
  225. $view->doctype('XHTML1_STRICT');
  226. // Set the initial title and separator:
  227. $view->headTitle('My Site')
  228. ->setSeparator(' :: ');
  229. // Set the initial stylesheet:
  230. $view->headLink()->prependStylesheet('/styles/site.css');
  231. }
  232. // ...
  233. }
  234. ]]></programlisting>
  235. <para>
  236. Later, in a controller or action-specific view script, we can add more stylesheets:
  237. </para>
  238. <programlisting language="php"><![CDATA[
  239. <?php $this->headLink()->appendStylesheet('/styles/user-list.css') ?>
  240. ]]></programlisting>
  241. <para>
  242. Within our layout view script, once again, we simply echo the placeholder:
  243. </para>
  244. <programlisting language="php"><![CDATA[
  245. <?php echo $this->doctype() ?>
  246. <html>
  247. <?php echo $this->headTitle() ?>
  248. <?php echo $this->headLink() ?>
  249. <!-- ... -->
  250. ]]></programlisting>
  251. <para>
  252. This will generate the following output:
  253. </para>
  254. <programlisting language="html"><![CDATA[
  255. <link rel="stylesheet" type="text/css" href="/styles/site.css" />
  256. <link rel="stylesheet" type="text/css" href="/styles/user-list.css" />
  257. ]]></programlisting>
  258. </sect2>
  259. <sect2 id="learning.view.placeholders.standard.head-script">
  260. <title>Aggregating Scripts Using HeadScript</title>
  261. <para>
  262. Another common tactic to prevent long page load times is to only load JavaScript when
  263. necessary. That said, you may need several layers of scripts: perhaps one for
  264. progressively enhancing menus on the site, and another for page-specific content. In
  265. these situations, the <methodname>headScript()</methodname> helper presents a solution.
  266. </para>
  267. <para>
  268. Similar to the <methodname>headLink()</methodname> helper,
  269. <methodname>headScript()</methodname> provides the ability to append or prepend scripts
  270. to the collection, and then echo the entire set. It provides the flexibility to specify
  271. either script files themselves to load, or explicit JavaScript. You also have the option
  272. of capturing JavaScript via
  273. <methodname>captureStart()</methodname>/<methodname>captureEnd()</methodname>, which
  274. allows you to simply inline the JavaScript instead of requiring an additional call to
  275. your server.
  276. </para>
  277. <para>
  278. Also like <methodname>headLink()</methodname>, <methodname>headScript</methodname>
  279. provides "virtual" methods via overloading as a convenience when specifying items to
  280. aggregate; common methods include <methodname>prependFile()</methodname>,
  281. <methodname>appendFile()</methodname>, <methodname>prependScript()</methodname>, and
  282. <methodname>appendScript()</methodname>. The first two allow you to specify files that
  283. will be referenced in a <code>&lt;script&gt;</code> tag's <varname>src</varname>
  284. attribute; the latter two will take the content provided and render it as literal
  285. JavaScript within a <code>&lt;script&gt;</code> tag.
  286. </para>
  287. <para>
  288. In this example, we'll specify that a script, "/js/site.js" needs to be loaded on every
  289. page; we'll update our <methodname>_initPlaceholders()</methodname> bootstrap method to
  290. do this.
  291. </para>
  292. <programlisting language="php"><![CDATA[
  293. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  294. {
  295. // ...
  296. protected function _initPlaceholders()
  297. {
  298. $this->bootstrap('View');
  299. $view = $this->getResource('View');
  300. $view->doctype('XHTML1_STRICT');
  301. // Set the initial title and separator:
  302. $view->headTitle('My Site')
  303. ->setSeparator(' :: ');
  304. // Set the initial stylesheet:
  305. $view->headLink()->prependStylesheet('/styles/site.css');
  306. // Set the initial JS to load:
  307. $view->headScript()->prependFile('/js/site.js');
  308. }
  309. // ...
  310. }
  311. ]]></programlisting>
  312. <para>
  313. Within a view script, we might then add an extra script file to source, or capture some
  314. JavaScript to include in our document.
  315. </para>
  316. <programlisting language="php"><![CDATA[
  317. <?php $this->headScript()->appendFile('/js/user-list.js') ?>
  318. <?php $this->headScript()->captureStart() ?>
  319. site = {
  320. baseUrl: "<?php echo $this->baseUrl() ?>"
  321. };
  322. <?php $this->headScript()->captureEnd() ?>
  323. ]]></programlisting>
  324. <para>
  325. Within our layout script, we then simply echo the placeholder, just as we have all the
  326. others:
  327. </para>
  328. <programlisting language="php"><![CDATA[
  329. <?php echo $this->doctype() ?>
  330. <html>
  331. <?php echo $this->headTitle() ?>
  332. <?php echo $this->headLink() ?>
  333. <?php echo $this->headScript() ?>
  334. <!-- ... -->
  335. ]]></programlisting>
  336. <para>
  337. This will generate the following output:
  338. </para>
  339. <programlisting language="html"><![CDATA[
  340. <script type="text/javascript" src="/js/site.js"></script>
  341. <script type="text/javascript" src="/js/user-list.js"></script>
  342. <script type="text/javascript">
  343. site = {
  344. baseUrl: "<?php echo $this->baseUrl() ?>"
  345. };
  346. </script>
  347. ]]></programlisting>
  348. <note>
  349. <title>InlineScript Variant</title>
  350. <para>
  351. Many browsers will often block display of a page until all scripts and stylesheets
  352. referenced in the <code>&lt;head&gt;</code> section have loaded. If you have a
  353. number of such directives, this can impact how soon somebody can start actually
  354. viewing the page.
  355. </para>
  356. <para>
  357. One way around this is to emit your <code>&lt;script&gt;</code> tags just prior to
  358. closing the <code>&lt;body&gt;</code> of your document. (This is a practice
  359. specifically recommend by the <ulink
  360. url="http://developer.yahoo.com/yslow/">Y! Slow project</ulink>.)
  361. </para>
  362. <para>
  363. Zend Framework supports this in two different ways:
  364. </para>
  365. <itemizedlist>
  366. <listitem>
  367. <para>
  368. You can render your <methodname>headScript()</methodname> tag whereever you
  369. like in your layout script; just because the title references "head" does
  370. not mean it needs to be rendered in that location.
  371. </para>
  372. </listitem>
  373. <listitem>
  374. <para>
  375. Alternately, you may use the <methodname>inlineScript()</methodname> helper,
  376. which is simply a variant on <methodname>headScript()</methodname>, and
  377. retains the same behavior, but uses a separate registry.
  378. </para>
  379. </listitem>
  380. </itemizedlist>
  381. </note>
  382. </sect2>
  383. </sect1>