Zend_View-Helpers-HeadStyle.xml 6.4 KB

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