<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="learning.layout.usage">
    <title>Using Zend_Layout</title>

    <para>
        Basic usage of <classname>Zend_Layout</classname> is fairly trivial. Assuming you're using
        <classname>Zend_Application</classname> already, you can simply provide some configuration
        options and create a layout view script.
    </para>

    <sect2 id="learning.layout.usage.configuration">
        <title>Layout Configuration</title>

        <para>
            The recommended location of layouts is in a "<filename>layouts/scripts/</filename>"
            subdirectory of your application:
        </para>

        <programlisting language="text"><![CDATA[
application
|-- Bootstrap.php
|-- configs
|   `-- application.ini
|-- controllers
|-- layouts
|   `-- scripts
|       |-- layout.phtml
]]></programlisting>

        <para>
            To initialize <classname>Zend_Layout</classname>, add the following to your
            configuration file ("<filename>application/configs/application.ini</filename>"):
        </para>

        <programlisting language="dosini"><![CDATA[
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.layout.layout = "layout"
]]></programlisting>

        <para>
            The first line indicates where to look for layout scripts; the second line gives the
            name of the layout to use, minus the view script extension (which is assumed to be
            "<filename>.phtml</filename>" by default).
        </para>
    </sect2>

    <sect2 id="learning.layout.usage.layout-script">
        <title>Create a Layout Script</title>

        <para>
            Now that you have your configuration in place, you need to create your layout script.
            First, make sure that you've created the
            "<filename>application/layouts/scripts</filename>" directory; then,
            open an editor, and create the markup for your layout. Layout scripts are simply view
            scripts, with some slight differences.
        </para>

        <programlisting language="php"><![CDATA[
<html>
<head>
    <title>My Site</title>
</head>
<body>
    <?php echo $this->layout()->content ?>
</body>
</html>
]]></programlisting>

        <para>
            In the example above, you'll note the call to a <methodname>layout()</methodname> view
            helper. When you register the <classname>Zend_Layout</classname> resource, you also gain
            access to both an action and view helper that allow you access to the
            <classname>Zend_Layout</classname> instance; you can then call operations on the layout
            object. In this case, we're retrieving a named variable, <varname>$content</varname>,
            and echoing it. By default, the <varname>$content</varname> variable is populated for
            you from the application view script rendered. Otherwise, anything you'd normally do
            in a view script is perfectly valid -- call any helpers or view methods you desire.
        </para>

        <para>
            At this point, you have a working layout script, and your application is informed of its
            location and knows to render it.
        </para>
    </sect2>

    <sect2 id="learning.layout.usage.access">
        <title>Accessing the Layout Object</title>

        <para>
            On occasion, you may need direct access to the layout object. There are three ways you
            can do this:
        </para>

        <itemizedlist>
            <listitem>
                <para>
                    <emphasis>Within view scripts:</emphasis> use the
                    <methodname>layout()</methodname> view helper, which returns the
                    <classname>Zend_Layout</classname> instance registered with the front controller
                    plugin.
                </para>

                <programlisting language="php"><![CDATA[
<?php $layout = $this->layout(); ?>
]]></programlisting>

                <para>
                    Since it returns the layout instance, you can also simply call methods on it,
                    rather than assigning it to a variable.
                </para>
            </listitem>

            <listitem>
                <para>
                    <emphasis>Within action controllers:</emphasis> use the
                    <methodname>layout()</methodname> action helper, which acts just like the view
                    helper.
                </para>

                <programlisting language="php"><![CDATA[
// Calling helper as a method of the helper broker:
$layout = $this->_helper->layout();

// Or, more verbosely:
$helper = $this->_helper->getHelper('Layout');
$layout = $helper->getLayoutInstance();
]]></programlisting>

                <para>
                    As with the view helper, since the action helper returns the layout instance,
                    you can also simply call methods on it, rather than assigning it to a variable.
                </para>
            </listitem>

            <listitem>
                <para>
                    <emphasis>Elsewhere: </emphasis> use the static method
                    <methodname>getMvcInstance()</methodname>. This will return the layout instance
                    registered by the bootstrap resource.
                </para>

                <programlisting language="php"><![CDATA[
$layout = Zend_Layout::getMvcInstance();
]]></programlisting>
            </listitem>

            <listitem>
                <para>
                    <emphasis>Via the bootstrap: </emphasis> retrieve the layout resource, which
                    will be the <classname>Zend_Layout</classname> instance.
                </para>

                <programlisting language="php"><![CDATA[
$layout = $bootstrap->getResource('Layout');
]]></programlisting>

                <para>
                    Anywhere you have access to the bootstrap object, this method is preferred over
                    using the static <methodname>getMvcInstance()</methodname> method.
                </para>
            </listitem>
        </itemizedlist>
    </sect2>

    <sect2 id="learning.layout.usage.other-operations">
        <title>Other Operations</title>

        <para>
            In most cases, the above configuration and layout script (with modifications) will get
            you what you need. However, some other functionality exists you will likely use sooner
            or later. In all of the following examples, you may use one of the <link
                linkend="learning.layout.usage.access">methods listed above</link> for retrieving
            the layout object.
        </para>

        <itemizedlist>
            <listitem>
                <para>
                    <emphasis>Setting layout variables</emphasis>.
                    <classname>Zend_Layout</classname> keeps its own registry of layout-specific
                    view variables that you can access; the <varname>$content</varname> key noted in
                    the initial layout script sample is one such example. You can assign and
                    retrieve these using normal property access, or via the
                    <methodname>assign()</methodname> method.
                </para>

                <programlisting language="php"><![CDATA[
// Setting content:
$layout->somekey = "foo"

// Echoing that same content:
echo $layout->somekey; // 'foo'

// Using the assign() method:
$layout->assign('someotherkey', 'bar');

// Access to assign()'d variables remains the same:
echo $layout->someotherkey; // 'bar'
]]></programlisting>
            </listitem>

            <listitem>
                <para>
                    <methodname>disableLayout()</methodname>. Occasionally, you may want to disable
                    layouts; for example, when answering an Ajax request, or providing a RESTful
                    representation of a resource. In these cases, you can call the
                    <methodname>disableLayout()</methodname> method on your layout object.
                </para>

                <programlisting language="php"><![CDATA[
$layout->disableLayout();
]]></programlisting>

                <para>
                    The opposite of this method is, of course,
                    <methodname>enableLayout()</methodname>, which can be called at any time to
                    re-enable layouts for the requested action.
                </para>
            </listitem>

            <listitem>
                <para>
                    <emphasis>Selecting an alternate layout</emphasis>: If you have multiple
                    layouts for your site or application, you can select the layout to use at any
                    time by simply calling the <methodname>setLayout()</methodname> method. Call it
                    by specifying the name of the layout script without the file suffix.
                </para>

                <programlisting language="php"><![CDATA[
// Use the layout script "alternate.phtml":
$layout->setLayout('alternate');
]]></programlisting>

                <para>
                    The layout script should reside in the <varname>$layoutPath</varname> directory
                    specified in your configuration. <classname>Zend_Layout</classname> will then
                    use this new layout when rendering.
                </para>
            </listitem>
        </itemizedlist>
    </sect2>
</sect1>
