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
|
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect3 id="zend.view.helpers.initial.headstyle">
<title>HeadStyle Helper</title>
<para>
The <acronym>HTML</acronym> <emphasis><style></emphasis> element is used to include
<acronym>CSS</acronym> stylesheets inline in the <acronym>HTML</acronym>
<emphasis><head></emphasis> element.
</para>
<note>
<title>Use HeadLink to link CSS files</title>
<para>
<link linkend="zend.view.helpers.initial.headlink">HeadLink</link>
should be used to create <emphasis><link></emphasis> elements for
including external stylesheets. <classname>HeadStyle</classname> is used when
you wish to define your stylesheets inline.
</para>
</note>
<para>
The <classname>HeadStyle</classname> helper supports the following methods for
setting and adding stylesheet declarations:
</para>
<itemizedlist>
<listitem>
<para>
<command>appendStyle($content, $attributes = array())</command>
</para>
</listitem>
<listitem>
<para>
<command>offsetSetStyle($index, $content, $attributes = array())</command>
</para>
</listitem>
<listitem>
<para>
<command>prependStyle($content, $attributes = array())</command>
</para>
</listitem>
<listitem>
<para>
<command>setStyle($content, $attributes = array())</command>
</para>
</listitem>
</itemizedlist>
<para>
In all cases, <varname>$content</varname> is the actual <acronym>CSS</acronym> declarations.
<varname>$attributes</varname> are any additional attributes you wish to provide to the
<property>style</property> tag: lang, title, media, or dir are all permissible.
</para>
<note>
<title>Setting Conditional Comments</title>
<para>
<classname>HeadStyle</classname> allows you to wrap the style 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>$attributes</varname> parameter
in the method calls.
</para>
<example id="zend.view.helpers.initial.headstyle.conditional">
<title>Headstyle With Conditional Comments</title>
<programlisting language="php"><![CDATA[
// adding scripts
$this->headStyle()->appendStyle($styles, array('conditional' => 'lt IE 7'));
]]></programlisting>
</example>
</note>
<para>
<classname>HeadStyle</classname> also allows capturing style declarations; this
can be useful if you want to create the declarations programmatically,
and then place them elsewhere. The usage for this will be showed in an
example below.
</para>
<para>
Finally, you can also use the <methodname>headStyle()</methodname> method to
quickly add declarations elements; the signature for this is
<methodname>headStyle($content$placement = 'APPEND', $attributes = array())</methodname>.
<varname>$placement</varname> should be either 'APPEND', 'PREPEND', or 'SET'.
</para>
<para>
<classname>HeadStyle</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>HeadStyle</classname> helper is a concrete implementation of the
<link linkend="zend.view.helpers.initial.placeholder">Placeholder
helper</link>.
</para>
<note>
<title>UTF-8 encoding used by default</title>
<para>
By default, Zend Framework uses <acronym>UTF-8</acronym> as its default encoding, and,
specific to this case, <classname>Zend_View</classname> does as well. Character encoding
can be set differently on the view object itself using the
<methodname>setEncoding()</methodname> method (or the <varname>encoding</varname>
instantiation parameter). However, since <classname>Zend_View_Interface</classname> does
not define accessors for encoding, it's possible that if you are using a custom view
implementation with this view helper, you will not have a
<methodname>getEncoding()</methodname> method, which is what the view helper uses
internally for determining the character set in which to encode.
</para>
<para>
If you do not want to utilize <acronym>UTF-8</acronym> in such a situation, you will
need to implement a <methodname>getEncoding()</methodname> method in your custom view
implementation.
</para>
</note>
<example id="zend.view.helpers.initial.headstyle.basicusage">
<title>HeadStyle Helper Basic Usage</title>
<para>
You may specify a new style tag at any time:
</para>
<programlisting language="php"><![CDATA[
// adding styles
$this->headStyle()->appendStyle($styles);
]]></programlisting>
<para>
Order is very important with <acronym>CSS</acronym>; you may need to ensure that
declarations are loaded in a specific order due to the order of the
cascade; use the various append, prepend, and offsetSet directives
to aid in this task:
</para>
<programlisting language="php"><![CDATA[
// Putting styles in order
// place at a particular offset:
$this->headStyle()->offsetSetStyle(100, $customStyles);
// place at end:
$this->headStyle()->appendStyle($finalStyles);
// place at beginning
$this->headStyle()->prependStyle($firstStyles);
]]></programlisting>
<para>
When you're finally ready to output all style declarations in your
layout script, simply echo the helper:
</para>
<programlisting language="php"><![CDATA[
<?php echo $this->headStyle() ?>
]]></programlisting>
</example>
<example id="zend.view.helpers.initial.headstyle.capture">
<title>Capturing Style Declarations Using the HeadStyle Helper</title>
<para>
Sometimes you need to generate <acronym>CSS</acronym> style declarations
programmatically. While you could use string concatenation,
heredocs, and the like, often it's easier just to do so by creating
the styles and sprinkling in <acronym>PHP</acronym> tags.
<classname>HeadStyle</classname> lets you do just that, capturing it to the stack:
</para>
<programlisting language="php"><![CDATA[
<?php $this->headStyle()->captureStart() ?>
body {
background-color: <?php echo $this->bgColor ?>;
}
<?php $this->headStyle()->captureEnd() ?>
]]></programlisting>
<para>
The following assumptions are made:
</para>
<itemizedlist>
<listitem>
<para>
The style declarations will be appended to the stack. If you
wish for them 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>
If you wish to specify any additional attributes for the
<emphasis><style></emphasis> tag, pass them in an array as
the second argument to <methodname>captureStart()</methodname>.
</para>
</listitem>
</itemizedlist>
</example>
</sect3>
<!--
vim:se ts=4 sw=4 et:
-->
|