File: Zend_View-Helpers-HeadScript.xml

package info (click to toggle)
zendframework 1.12.9%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 133,584 kB
  • sloc: xml: 1,311,829; php: 570,173; sh: 170; makefile: 125; sql: 121
file content (291 lines) | stat: -rw-r--r-- 11,180 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect3 id="zend.view.helpers.initial.headscript">
    <title>HeadScript Helper</title>

    <para>
        The <acronym>HTML</acronym> <emphasis>&lt;script&gt;</emphasis> element is used to either
        provide inline client-side scripting elements or link to a remote resource
        containing client-side scripting code. The <classname>HeadScript</classname>
        helper allows you to manage both.
    </para>

    <para>
        The <classname>HeadScript</classname> helper supports the following methods for
        setting and adding scripts:
    </para>

    <itemizedlist>
        <listitem>
            <para>
                <command>appendFile($src, $type = 'text/javascript', $attrs = array())</command>
            </para>
        </listitem>

        <listitem>
            <para>
                <command>offsetSetFile($index, $src, $type = 'text/javascript', $attrs =
                    array())</command>
            </para>
        </listitem>

        <listitem>
            <para>
                <command>prependFile($src, $type = 'text/javascript', $attrs = array())</command>
            </para>
        </listitem>

        <listitem>
            <para>
                <command>setFile($src, $type = 'text/javascript', $attrs = array())</command>
            </para>
        </listitem>

        <listitem>
            <para>
                <command>appendScript($script, $type = 'text/javascript', $attrs =
                    array())</command>
            </para>
        </listitem>

        <listitem>
            <para>
                <command>offsetSetScript($index, $script, $type = 'text/javascript', $attrs =
                    array())</command>
            </para>
        </listitem>

        <listitem>
            <para>
                <command>prependScript($script, $type = 'text/javascript', $attrs =
                    array())</command>
            </para>
        </listitem>

        <listitem>
            <para>
                <command>setScript($script, $type = 'text/javascript', $attrs = array())</command>
            </para>
        </listitem>
    </itemizedlist>

    <para>
        In the case of the *<methodname>File()</methodname> methods, <varname>$src</varname> is
        the remote location of the script to load; this is usually in the form
        of a <acronym>URL</acronym> or a path. For the *<methodname>Script()</methodname> methods,
        <varname>$script</varname> is the client-side scripting directives you wish to
        use in the element.
    </para>

    <note>
        <title>Setting Conditional Comments</title>

        <para>
            <classname>HeadScript</classname> allows you to wrap the script tag in conditional
            comments, which allows you to hide it from specific browsers. To add the conditional
            tags, pass the conditional value as part of the <varname>$attrs</varname> parameter in
            the method calls.
        </para>

        <example id="zend.view.helpers.initial.headscript.conditional">
            <title>Headscript With Conditional Comments</title>

            <programlisting language="php"><![CDATA[
// adding scripts
$this->headScript()->appendFile(
    '/js/prototype.js',
    'text/javascript',
    array('conditional' => 'lt IE 7')
);
]]></programlisting>
        </example>
    </note>

    <note>
        <title>Preventing HTML style comments or CDATA wrapping of scripts</title>

        <para>
            By default <classname>HeadScript</classname> will wrap scripts with HTML
            comments or it wraps scripts with XHTML cdata. This behavior can be
            problematic when you intend to use the script tag in an alternative way by
            setting the type to something other then 'text/javascript'. To prevent such
            escaping, pass an <varname>noescape</varname> with a value of true as part of
            the <varname>$attrs</varname> parameter in the method calls.
        </para>

        <example id="zend.view.helpers.initial.headscript.noescape">
            <title>Create an jQuery template with the headScript</title>

            <programlisting language="php"><![CDATA[
// jquery template
$template = '<div class="book">{{:title}}</div>';
$this->headScript()->appendScript(
    $template,
    'text/x-jquery-tmpl',
    array('id='tmpl-book', 'noescape' => true)
);
]]></programlisting>
        </example>
    </note>

    <para>
        <classname>HeadScript</classname> also allows capturing scripts; this can be
        useful if you want to create the client-side script programmatically,
        and then place it elsewhere. The usage for this will be showed in an
        example below.
    </para>

    <para>
        Finally, you can also use the <methodname>headScript()</methodname> method to
        quickly add script elements; the signature for this is
        <methodname>headScript($mode = 'FILE', $spec, $placement = 'APPEND')</methodname>.
        The <varname>$mode</varname> is either 'FILE' or 'SCRIPT', depending on if
        you're linking a script or defining one. <varname>$spec</varname> is either
        the script file to link or the script source itself.
        <varname>$placement</varname> should be either 'APPEND', 'PREPEND', or 'SET'.
    </para>

    <para>
        <classname>HeadScript</classname> overrides each of <methodname>append()</methodname>,
        <methodname>offsetSet()</methodname>, <methodname>prepend()</methodname>, and
        <methodname>set()</methodname> to enforce usage of the special methods as listed above.
        Internally, it stores each item as a <property>stdClass</property> token, which it later
        serializes using the <methodname>itemToString()</methodname> method. This allows you
        to perform checks on the items in the stack, and optionally modify these
        items by simply modifying the object returned.
    </para>

    <para>
        The <classname>HeadScript</classname> helper is a concrete implementation of the
        <link linkend="zend.view.helpers.initial.placeholder">Placeholder helper</link>.
    </para>

    <note>
        <title>Use InlineScript for HTML Body Scripts</title>

        <para>
            <classname>HeadScript</classname>'s sibling helper, <link
                linkend="zend.view.helpers.initial.inlinescript">InlineScript</link>,
            should be used when you wish to include scripts inline in the <acronym>HTML</acronym>
            <emphasis>body</emphasis>. Placing scripts at the end of your document is a
            good practice for speeding up delivery of your page, particularly
            when using 3rd party analytics scripts.
        </para>
    </note>

    <note>
        <title>Arbitrary Attributes are Disabled by Default</title>

        <para>
            By default, <classname>HeadScript</classname> only will render
            <emphasis>&lt;script&gt;</emphasis> attributes that are blessed by the W3C.
            These include 'type', 'charset', 'defer', 'language', and 'src'.
            However, some javascript frameworks, notably <ulink
                url="http://www.dojotoolkit.org/">Dojo</ulink>, utilize custom
            attributes in order to modify behavior. To allow such attributes,
            you can enable them via the
            <methodname>setAllowArbitraryAttributes()</methodname> method:
        </para>

        <programlisting language="php"><![CDATA[
$this->headScript()->setAllowArbitraryAttributes(true);
]]></programlisting>
    </note>

    <example id="zend.view.helpers.initial.headscript.basicusage">
        <title>HeadScript Helper Basic Usage</title>

        <para>
            You may specify a new script tag at any time. As noted above, these
            may be links to outside resource files or scripts themselves.
        </para>

        <programlisting language="php"><![CDATA[
// adding scripts
$this->headScript()->appendFile('/js/prototype.js')
                   ->appendScript($onloadScript);
]]></programlisting>

        <para>
            Order is often important with client-side scripting; you may need to
            ensure that libraries are loaded in a specific order due to
            dependencies each have; use the various append, prepend, and
            offsetSet directives to aid in this task:
        </para>

        <programlisting language="php"><![CDATA[
// Putting scripts in order

// place at a particular offset to ensure loaded last
$this->headScript()->offsetSetFile(100, '/js/myfuncs.js');

// use scriptaculous effects (append uses next index, 101)
$this->headScript()->appendFile('/js/scriptaculous.js');

// but always have base prototype script load first:
$this->headScript()->prependFile('/js/prototype.js');
]]></programlisting>

        <para>
            When you're finally ready to output all scripts in your layout
            script, simply echo the helper:
        </para>

        <programlisting language="php"><![CDATA[
<?php echo $this->headScript() ?>
]]></programlisting>
    </example>

    <example id="zend.view.helpers.initial.headscript.capture">
        <title>Capturing Scripts Using the HeadScript Helper</title>

        <para>
            Sometimes you need to generate client-side scripts programmatically.
            While you could use string concatenation, heredocs, and the like,
            often it's easier just to do so by creating the script and
            sprinkling in <acronym>PHP</acronym> tags. <classname>HeadScript</classname> lets you do
            just that, capturing it to the stack:
        </para>

        <programlisting language="php"><![CDATA[
<?php $this->headScript()->captureStart() ?>
var action = '<?php echo $this->baseUrl ?>';
$('foo_form').action = action;
<?php $this->headScript()->captureEnd() ?>
]]></programlisting>

        <para>
            The following assumptions are made:
        </para>

        <itemizedlist>
            <listitem>
                <para>
                    The script will be appended to the stack. If you wish for it
                    to replace the stack or be added to the top, you will need
                    to pass 'SET' or 'PREPEND', respectively, as the first
                    argument to <methodname>captureStart()</methodname>.
                </para>
            </listitem>

            <listitem>
                <para>
                    The script <acronym>MIME</acronym> type is assumed to be 'text/javascript'; if
                    you wish to specify a different type, you will need to pass it
                    as the second argument to <methodname>captureStart()</methodname>.
                </para>
            </listitem>

            <listitem>
                <para>
                    If you wish to specify any additional attributes for the
                    <emphasis>&lt;script&gt;</emphasis> tag, pass them in an array as
                    the third argument to <methodname>captureStart()</methodname>.
                </para>
            </listitem>
        </itemizedlist>
    </example>
</sect3>
<!--
vim:se ts=4 sw=4 et:
-->