view-placeholders-standard.xml 17 KB

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