Zend_View-Helpers-HeadScript.xml 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. <para>
  89. <classname>HeadScript</classname> also allows capturing scripts; this can be
  90. useful if you want to create the client-side script programmatically,
  91. and then place it elsewhere. The usage for this will be showed in an
  92. example below.
  93. </para>
  94. <para>
  95. Finally, you can also use the <methodname>headScript()</methodname> method to
  96. quickly add script elements; the signature for this is
  97. <methodname>headScript($mode = 'FILE', $spec, $placement = 'APPEND')</methodname>.
  98. The <varname>$mode</varname> is either 'FILE' or 'SCRIPT', depending on if
  99. you're linking a script or defining one. <varname>$spec</varname> is either
  100. the script file to link or the script source itself.
  101. <varname>$placement</varname> should be either 'APPEND', 'PREPEND', or 'SET'.
  102. </para>
  103. <para>
  104. <classname>HeadScript</classname> overrides each of <methodname>append()</methodname>,
  105. <methodname>offsetSet()</methodname>, <methodname>prepend()</methodname>, and
  106. <methodname>set()</methodname> to enforce usage of the special methods as listed above.
  107. Internally, it stores each item as a <property>stdClass</property> token, which it later
  108. serializes using the <methodname>itemToString()</methodname> method. This allows you
  109. to perform checks on the items in the stack, and optionally modify these
  110. items by simply modifying the object returned.
  111. </para>
  112. <para>
  113. The <classname>HeadScript</classname> helper is a concrete implementation of the
  114. <link linkend="zend.view.helpers.initial.placeholder">Placeholder helper</link>.
  115. </para>
  116. <note>
  117. <title>Use InlineScript for HTML Body Scripts</title>
  118. <para>
  119. <classname>HeadScript</classname>'s sibling helper, <link
  120. linkend="zend.view.helpers.initial.inlinescript">InlineScript</link>,
  121. should be used when you wish to include scripts inline in the <acronym>HTML</acronym>
  122. <emphasis>body</emphasis>. Placing scripts at the end of your document is a
  123. good practice for speeding up delivery of your page, particularly
  124. when using 3rd party analytics scripts.
  125. </para>
  126. </note>
  127. <note>
  128. <title>Arbitrary Attributes are Disabled by Default</title>
  129. <para>
  130. By default, <classname>HeadScript</classname> only will render
  131. <emphasis>&lt;script&gt;</emphasis> attributes that are blessed by the W3C.
  132. These include 'type', 'charset', 'defer', 'language', and 'src'.
  133. However, some javascript frameworks, notably <ulink
  134. url="http://www.dojotoolkit.org/">Dojo</ulink>, utilize custom
  135. attributes in order to modify behavior. To allow such attributes,
  136. you can enable them via the
  137. <methodname>setAllowArbitraryAttributes()</methodname> method:
  138. </para>
  139. <programlisting language="php"><![CDATA[
  140. $this->headScript()->setAllowArbitraryAttributes(true);
  141. ]]></programlisting>
  142. </note>
  143. <example id="zend.view.helpers.initial.headscript.basicusage">
  144. <title>HeadScript Helper Basic Usage</title>
  145. <para>
  146. You may specify a new script tag at any time. As noted above, these
  147. may be links to outside resource files or scripts themselves.
  148. </para>
  149. <programlisting language="php"><![CDATA[
  150. // adding scripts
  151. $this->headScript()->appendFile('/js/prototype.js')
  152. ->appendScript($onloadScript);
  153. ]]></programlisting>
  154. <para>
  155. Order is often important with client-side scripting; you may need to
  156. ensure that libraries are loaded in a specific order due to
  157. dependencies each have; use the various append, prepend, and
  158. offsetSet directives to aid in this task:
  159. </para>
  160. <programlisting language="php"><![CDATA[
  161. // Putting scripts in order
  162. // place at a particular offset to ensure loaded last
  163. $this->headScript()->offsetSetFile(100, '/js/myfuncs.js');
  164. // use scriptaculous effects (append uses next index, 101)
  165. $this->headScript()->appendFile('/js/scriptaculous.js');
  166. // but always have base prototype script load first:
  167. $this->headScript()->prependFile('/js/prototype.js');
  168. ]]></programlisting>
  169. <para>
  170. When you're finally ready to output all scripts in your layout
  171. script, simply echo the helper:
  172. </para>
  173. <programlisting language="php"><![CDATA[
  174. <?php echo $this->headScript() ?>
  175. ]]></programlisting>
  176. </example>
  177. <example id="zend.view.helpers.initial.headscript.capture">
  178. <title>Capturing Scripts Using the HeadScript Helper</title>
  179. <para>
  180. Sometimes you need to generate client-side scripts programmatically.
  181. While you could use string concatenation, heredocs, and the like,
  182. often it's easier just to do so by creating the script and
  183. sprinkling in <acronym>PHP</acronym> tags. <classname>HeadScript</classname> lets you do
  184. just that, capturing it to the stack:
  185. </para>
  186. <programlisting language="php"><![CDATA[
  187. <?php $this->headScript()->captureStart() ?>
  188. var action = '<?php echo $this->baseUrl ?>';
  189. $('foo_form').action = action;
  190. <?php $this->headScript()->captureEnd() ?>
  191. ]]></programlisting>
  192. <para>
  193. The following assumptions are made:
  194. </para>
  195. <itemizedlist>
  196. <listitem>
  197. <para>
  198. The script will be appended to the stack. If you wish for it
  199. to replace the stack or be added to the top, you will need
  200. to pass 'SET' or 'PREPEND', respectively, as the first
  201. argument to <methodname>captureStart()</methodname>.
  202. </para>
  203. </listitem>
  204. <listitem>
  205. <para>
  206. The script <acronym>MIME</acronym> type is assumed to be 'text/javascript'; if
  207. you wish to specify a different type, you will need to pass it
  208. as the second argument to <methodname>captureStart()</methodname>.
  209. </para>
  210. </listitem>
  211. <listitem>
  212. <para>
  213. If you wish to specify any additional attributes for the
  214. <emphasis>&lt;script&gt;</emphasis> tag, pass them in an array as
  215. the third argument to <methodname>captureStart()</methodname>.
  216. </para>
  217. </listitem>
  218. </itemizedlist>
  219. </example>
  220. </sect3>
  221. <!--
  222. vim:se ts=4 sw=4 et:
  223. -->