Zend_View-Helpers-HeadStyle.xml 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect3 id="zend.view.helpers.initial.headstyle">
  4. <title>HeadStyle Helper</title>
  5. <para>
  6. The <acronym>HTML</acronym> <emphasis>&lt;style&gt;</emphasis> element is used to include
  7. <acronym>CSS</acronym> stylesheets inline in the <acronym>HTML</acronym>
  8. <emphasis>&lt;head&gt;</emphasis> element.
  9. </para>
  10. <note>
  11. <title>Use HeadLink to link CSS files</title>
  12. <para>
  13. <link linkend="zend.view.helpers.initial.headlink">HeadLink</link>
  14. should be used to create <emphasis>&lt;link&gt;</emphasis> elements for
  15. including external stylesheets. <classname>HeadStyle</classname> is used when
  16. you wish to define your stylesheets inline.
  17. </para>
  18. </note>
  19. <para>
  20. The <classname>HeadStyle</classname> helper supports the following methods for
  21. setting and adding stylesheet declarations:
  22. </para>
  23. <itemizedlist>
  24. <listitem>
  25. <para>
  26. <command>appendStyle($content, $attributes = array())</command>
  27. </para>
  28. </listitem>
  29. <listitem>
  30. <para>
  31. <command>offsetSetStyle($index, $content, $attributes = array())</command>
  32. </para>
  33. </listitem>
  34. <listitem>
  35. <para>
  36. <command>prependStyle($content, $attributes = array())</command>
  37. </para>
  38. </listitem>
  39. <listitem>
  40. <para>
  41. <command>setStyle($content, $attributes = array())</command>
  42. </para>
  43. </listitem>
  44. </itemizedlist>
  45. <para>
  46. In all cases, <varname>$content</varname> is the actual <acronym>CSS</acronym> declarations.
  47. <varname>$attributes</varname> are any additional attributes you wish to provide to the
  48. <property>style</property> tag: lang, title, media, or dir are all permissible.
  49. </para>
  50. <note>
  51. <title>Setting Conditional Comments</title>
  52. <para>
  53. <classname>HeadStyle</classname> allows you to wrap the style tag in conditional
  54. comments, which allows you to hide it from specific browsers. To add the conditional
  55. tags, pass the conditional value as part of the <varname>$attributes</varname> parameter
  56. in the method calls.
  57. </para>
  58. <example id="zend.view.helpers.initial.headstyle.conditional">
  59. <title>Headstyle With Conditional Comments</title>
  60. <programlisting language="php"><![CDATA[
  61. // adding scripts
  62. $this->headStyle()->appendStyle($styles, array('conditional' => 'lt IE 7'));
  63. ]]></programlisting>
  64. </example>
  65. </note>
  66. <para>
  67. <classname>HeadStyle</classname> also allows capturing style declarations; this
  68. can be useful if you want to create the declarations programmatically,
  69. and then place them elsewhere. The usage for this will be showed in an
  70. example below.
  71. </para>
  72. <para>
  73. Finally, you can also use the <methodname>headStyle()</methodname> method to
  74. quickly add declarations elements; the signature for this is
  75. <methodname>headStyle($content$placement = 'APPEND', $attributes = array())</methodname>.
  76. <varname>$placement</varname> should be either 'APPEND', 'PREPEND', or 'SET'.
  77. </para>
  78. <para>
  79. <classname>HeadStyle</classname> overrides each of <methodname>append()</methodname>,
  80. <methodname>offsetSet()</methodname>, <methodname>prepend()</methodname>, and
  81. <methodname>set()</methodname> to enforce usage of the special methods as listed above.
  82. Internally, it stores each item as a <property>stdClass</property> token, which it later
  83. serializes using the <methodname>itemToString()</methodname> method. This allows you
  84. to perform checks on the items in the stack, and optionally modify these
  85. items by simply modifying the object returned.
  86. </para>
  87. <para>
  88. The <classname>HeadStyle</classname> helper is a concrete implementation of the
  89. <link linkend="zend.view.helpers.initial.placeholder">Placeholder
  90. helper</link>.
  91. </para>
  92. <note>
  93. <title>UTF-8 encoding used by default</title>
  94. <para>
  95. By default, Zend Framework uses <acronym>UTF-8</acronym> as its default encoding, and,
  96. specific to this case, <classname>Zend_View</classname> does as well. Character encoding
  97. can be set differently on the view object itself using the
  98. <methodname>setEncoding()</methodname> method (or the <varname>encoding</varname>
  99. instantiation parameter). However, since <classname>Zend_View_Interface</classname> does
  100. not define accessors for encoding, it's possible that if you are using a custom view
  101. implementation with this view helper, you will not have a
  102. <methodname>getEncoding()</methodname> method, which is what the view helper uses
  103. internally for determining the character set in which to encode.
  104. </para>
  105. <para>
  106. If you do not want to utilize <acronym>UTF-8</acronym> in such a situation, you will
  107. need to implement a <methodname>getEncoding()</methodname> method in your custom view
  108. implementation.
  109. </para>
  110. </note>
  111. <example id="zend.view.helpers.initial.headstyle.basicusage">
  112. <title>HeadStyle Helper Basic Usage</title>
  113. <para>
  114. You may specify a new style tag at any time:
  115. </para>
  116. <programlisting language="php"><![CDATA[
  117. // adding styles
  118. $this->headStyle()->appendStyle($styles);
  119. ]]></programlisting>
  120. <para>
  121. Order is very important with <acronym>CSS</acronym>; you may need to ensure that
  122. declarations are loaded in a specific order due to the order of the
  123. cascade; use the various append, prepend, and offsetSet directives
  124. to aid in this task:
  125. </para>
  126. <programlisting language="php"><![CDATA[
  127. // Putting styles in order
  128. // place at a particular offset:
  129. $this->headStyle()->offsetSetStyle(100, $customStyles);
  130. // place at end:
  131. $this->headStyle()->appendStyle($finalStyles);
  132. // place at beginning
  133. $this->headStyle()->prependStyle($firstStyles);
  134. ]]></programlisting>
  135. <para>
  136. When you're finally ready to output all style declarations in your
  137. layout script, simply echo the helper:
  138. </para>
  139. <programlisting language="php"><![CDATA[
  140. <?php echo $this->headStyle() ?>
  141. ]]></programlisting>
  142. </example>
  143. <example id="zend.view.helpers.initial.headstyle.capture">
  144. <title>Capturing Style Declarations Using the HeadStyle Helper</title>
  145. <para>
  146. Sometimes you need to generate <acronym>CSS</acronym> style declarations
  147. programmatically. While you could use string concatenation,
  148. heredocs, and the like, often it's easier just to do so by creating
  149. the styles and sprinkling in <acronym>PHP</acronym> tags.
  150. <classname>HeadStyle</classname> lets you do just that, capturing it to the stack:
  151. </para>
  152. <programlisting language="php"><![CDATA[
  153. <?php $this->headStyle()->captureStart() ?>
  154. body {
  155. background-color: <?php echo $this->bgColor ?>;
  156. }
  157. <?php $this->headStyle()->captureEnd() ?>
  158. ]]></programlisting>
  159. <para>
  160. The following assumptions are made:
  161. </para>
  162. <itemizedlist>
  163. <listitem>
  164. <para>
  165. The style declarations will be appended to the stack. If you
  166. wish for them to replace the stack or be added to the top,
  167. you will need to pass 'SET' or 'PREPEND', respectively, as
  168. the first argument to <methodname>captureStart()</methodname>.
  169. </para>
  170. </listitem>
  171. <listitem>
  172. <para>
  173. If you wish to specify any additional attributes for the
  174. <emphasis>&lt;style&gt;</emphasis> tag, pass them in an array as
  175. the second argument to <methodname>captureStart()</methodname>.
  176. </para>
  177. </listitem>
  178. </itemizedlist>
  179. </example>
  180. </sect3>
  181. <!--
  182. vim:se ts=4 sw=4 et:
  183. -->