| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?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:
- -->
|