Zend_View-Helpers-HeadScript.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect3 id="zend.view.helpers.initial.headscript">
  4. <title>HeadScript Helper</title>
  5. <para>
  6. The <acronym>HTML</acronym> <emphasis>&lt;script&gt;</emphasis> element is used to either
  7. provide inline client-side scripting elements or link to a remote resource
  8. containing client-side scripting code. The <classname>HeadScript</classname>
  9. helper allows you to manage both.
  10. </para>
  11. <para>
  12. The <classname>HeadScript</classname> helper supports the following methods for
  13. setting and adding scripts:
  14. </para>
  15. <itemizedlist>
  16. <listitem>
  17. <para>
  18. <command>appendFile($src, $type = 'text/javascript', $attrs = array())</command>
  19. </para>
  20. </listitem>
  21. <listitem>
  22. <para>
  23. <command>offsetSetFile($index, $src, $type = 'text/javascript', $attrs =
  24. array())</command>
  25. </para>
  26. </listitem>
  27. <listitem>
  28. <para>
  29. <command>prependFile($src, $type = 'text/javascript', $attrs = array())</command>
  30. </para>
  31. </listitem>
  32. <listitem>
  33. <para>
  34. <command>setFile($src, $type = 'text/javascript', $attrs = array())</command>
  35. </para>
  36. </listitem>
  37. <listitem>
  38. <para>
  39. <command>appendScript($script, $type = 'text/javascript', $attrs =
  40. array())</command>
  41. </para>
  42. </listitem>
  43. <listitem>
  44. <para>
  45. <command>offsetSetScript($index, $script, $type = 'text/javascript', $attrs =
  46. array())</command>
  47. </para>
  48. </listitem>
  49. <listitem>
  50. <para>
  51. <command>prependScript($script, $type = 'text/javascript', $attrs =
  52. array())</command>
  53. </para>
  54. </listitem>
  55. <listitem>
  56. <para>
  57. <command>setScript($script, $type = 'text/javascript', $attrs = array())</command>
  58. </para>
  59. </listitem>
  60. </itemizedlist>
  61. <para>
  62. In the case of the *<methodname>File()</methodname> methods, <varname>$src</varname> is
  63. the remote location of the script to load; this is usually in the form
  64. of a <acronym>URL</acronym> or a path. For the *<methodname>Script()</methodname> methods,
  65. <varname>$script</varname> is the client-side scripting directives you wish to
  66. use in the element.
  67. </para>
  68. <note>
  69. <title>Setting Conditional Comments</title>
  70. <para>
  71. <classname>HeadScript</classname> allows you to wrap the script tag in conditional
  72. comments, which allows you to hide it from specific browsers. To add the conditional
  73. tags, pass the conditional value as part of the <varname>$attrs</varname> parameter in
  74. the method calls.
  75. </para>
  76. <example id="zend.view.helpers.initial.headscript.conditional">
  77. <title>Headscript With Conditional Comments</title>
  78. <programlisting language="php"><![CDATA[
  79. // adding scripts
  80. $this->headScript()->appendFile(
  81. '/js/prototype.js',
  82. 'text/javascript',
  83. array('conditional' => 'lt IE 7')
  84. );
  85. ]]></programlisting>
  86. </example>
  87. </note>
  88. <note>
  89. <title>Preventing HTML style comments or CDATA wrapping of scripts</title>
  90. <para>
  91. By default <classname>HeadScript</classname> will wrap scripts with HTML
  92. comments or it wraps scripts with XHTML cdata. This behavior can be
  93. problematic when you intend to use the script tag in an alternative way by
  94. setting the type to something other then 'text/javascript'. To prevent such
  95. escaping, pass an <varname>noescape</varname> with a value of true as part of
  96. the <varname>$attrs</varname> parameter in the method calls.
  97. </para>
  98. <example id="zend.view.helpers.initial.headscript.noescape">
  99. <title>Create an jQuery template with the headScript</title>
  100. <programlisting language="php"><![CDATA[
  101. // jquery template
  102. $template = '<div class="book">{{:title}}</div>';
  103. $this->headScript()->appendScript(
  104. $template,
  105. 'text/x-jquery-tmpl',
  106. array('id='tmpl-book', 'noescape' => true)
  107. );
  108. ]]></programlisting>
  109. </example>
  110. </note>
  111. <para>
  112. <classname>HeadScript</classname> also allows capturing scripts; this can be
  113. useful if you want to create the client-side script programmatically,
  114. and then place it elsewhere. The usage for this will be showed in an
  115. example below.
  116. </para>
  117. <para>
  118. Finally, you can also use the <methodname>headScript()</methodname> method to
  119. quickly add script elements; the signature for this is
  120. <methodname>headScript($mode = 'FILE', $spec, $placement = 'APPEND')</methodname>.
  121. The <varname>$mode</varname> is either 'FILE' or 'SCRIPT', depending on if
  122. you're linking a script or defining one. <varname>$spec</varname> is either
  123. the script file to link or the script source itself.
  124. <varname>$placement</varname> should be either 'APPEND', 'PREPEND', or 'SET'.
  125. </para>
  126. <para>
  127. <classname>HeadScript</classname> overrides each of <methodname>append()</methodname>,
  128. <methodname>offsetSet()</methodname>, <methodname>prepend()</methodname>, and
  129. <methodname>set()</methodname> to enforce usage of the special methods as listed above.
  130. Internally, it stores each item as a <property>stdClass</property> token, which it later
  131. serializes using the <methodname>itemToString()</methodname> method. This allows you
  132. to perform checks on the items in the stack, and optionally modify these
  133. items by simply modifying the object returned.
  134. </para>
  135. <para>
  136. The <classname>HeadScript</classname> helper is a concrete implementation of the
  137. <link linkend="zend.view.helpers.initial.placeholder">Placeholder helper</link>.
  138. </para>
  139. <note>
  140. <title>Use InlineScript for HTML Body Scripts</title>
  141. <para>
  142. <classname>HeadScript</classname>'s sibling helper, <link
  143. linkend="zend.view.helpers.initial.inlinescript">InlineScript</link>,
  144. should be used when you wish to include scripts inline in the <acronym>HTML</acronym>
  145. <emphasis>body</emphasis>. Placing scripts at the end of your document is a
  146. good practice for speeding up delivery of your page, particularly
  147. when using 3rd party analytics scripts.
  148. </para>
  149. </note>
  150. <note>
  151. <title>Arbitrary Attributes are Disabled by Default</title>
  152. <para>
  153. By default, <classname>HeadScript</classname> only will render
  154. <emphasis>&lt;script&gt;</emphasis> attributes that are blessed by the W3C.
  155. These include 'type', 'charset', 'defer', 'language', and 'src'.
  156. However, some javascript frameworks, notably <ulink
  157. url="http://www.dojotoolkit.org/">Dojo</ulink>, utilize custom
  158. attributes in order to modify behavior. To allow such attributes,
  159. you can enable them via the
  160. <methodname>setAllowArbitraryAttributes()</methodname> method:
  161. </para>
  162. <programlisting language="php"><![CDATA[
  163. $this->headScript()->setAllowArbitraryAttributes(true);
  164. ]]></programlisting>
  165. </note>
  166. <example id="zend.view.helpers.initial.headscript.basicusage">
  167. <title>HeadScript Helper Basic Usage</title>
  168. <para>
  169. You may specify a new script tag at any time. As noted above, these
  170. may be links to outside resource files or scripts themselves.
  171. </para>
  172. <programlisting language="php"><![CDATA[
  173. // adding scripts
  174. $this->headScript()->appendFile('/js/prototype.js')
  175. ->appendScript($onloadScript);
  176. ]]></programlisting>
  177. <para>
  178. Order is often important with client-side scripting; you may need to
  179. ensure that libraries are loaded in a specific order due to
  180. dependencies each have; use the various append, prepend, and
  181. offsetSet directives to aid in this task:
  182. </para>
  183. <programlisting language="php"><![CDATA[
  184. // Putting scripts in order
  185. // place at a particular offset to ensure loaded last
  186. $this->headScript()->offsetSetFile(100, '/js/myfuncs.js');
  187. // use scriptaculous effects (append uses next index, 101)
  188. $this->headScript()->appendFile('/js/scriptaculous.js');
  189. // but always have base prototype script load first:
  190. $this->headScript()->prependFile('/js/prototype.js');
  191. ]]></programlisting>
  192. <para>
  193. When you're finally ready to output all scripts in your layout
  194. script, simply echo the helper:
  195. </para>
  196. <programlisting language="php"><![CDATA[
  197. <?php echo $this->headScript() ?>
  198. ]]></programlisting>
  199. </example>
  200. <example id="zend.view.helpers.initial.headscript.capture">
  201. <title>Capturing Scripts Using the HeadScript Helper</title>
  202. <para>
  203. Sometimes you need to generate client-side scripts programmatically.
  204. While you could use string concatenation, heredocs, and the like,
  205. often it's easier just to do so by creating the script and
  206. sprinkling in <acronym>PHP</acronym> tags. <classname>HeadScript</classname> lets you do
  207. just that, capturing it to the stack:
  208. </para>
  209. <programlisting language="php"><![CDATA[
  210. <?php $this->headScript()->captureStart() ?>
  211. var action = '<?php echo $this->baseUrl ?>';
  212. $('foo_form').action = action;
  213. <?php $this->headScript()->captureEnd() ?>
  214. ]]></programlisting>
  215. <para>
  216. The following assumptions are made:
  217. </para>
  218. <itemizedlist>
  219. <listitem>
  220. <para>
  221. The script will be appended to the stack. If you wish for it
  222. to replace the stack or be added to the top, you will need
  223. to pass 'SET' or 'PREPEND', respectively, as the first
  224. argument to <methodname>captureStart()</methodname>.
  225. </para>
  226. </listitem>
  227. <listitem>
  228. <para>
  229. The script <acronym>MIME</acronym> type is assumed to be 'text/javascript'; if
  230. you wish to specify a different type, you will need to pass it
  231. as the second argument to <methodname>captureStart()</methodname>.
  232. </para>
  233. </listitem>
  234. <listitem>
  235. <para>
  236. If you wish to specify any additional attributes for the
  237. <emphasis>&lt;script&gt;</emphasis> tag, pass them in an array as
  238. the third argument to <methodname>captureStart()</methodname>.
  239. </para>
  240. </listitem>
  241. </itemizedlist>
  242. </example>
  243. </sect3>
  244. <!--
  245. vim:se ts=4 sw=4 et:
  246. -->