2
0

Zend_View-Helpers-HeadScript.xml 9.0 KB

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