view-placeholders-standard.xml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 <acronym>CSS</acronym> developers will often create a general stylesheet for
  193. sitewide styles, and individual stylesheets for specific sections or pages of the
  194. website, and load these latter conditionally so as to decrease the amount of data
  195. needing to be transferred on each request. The <methodname>headLink()</methodname>
  196. placeholder makes such conditional aggregation of stylesheets trivial within your
  197. 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 <acronym>MIME</acronym> type, which defaults to
  206. "text/css"), <varname>$conditionalStylesheet</varname> (which can be used to specify a
  207. "condition" under which the stylesheet will be evaluated), and
  208. <varname>$extras</varname> (an associative array of key and value pairs, commonly used
  209. to specify a key for "media"). In most cases, you will only need to specify the first
  210. argument, the relative path to the stylesheet.
  211. </para>
  212. <para>
  213. In our example, we'll assume that all pages need to load the stylesheet located in
  214. "<filename>/styles/site.css</filename>" (relative to the document root); we'll specify
  215. this in our <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, "<filename>/js/site.js</filename>" needs
  289. to be loaded on every page; we'll update our
  290. <methodname>_initPlaceholders()</methodname> bootstrap method to 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>