Zend Framework の紹介

 Learning Zend Framework

appendix

 Zend Framework リファレンス


  • 第36章 Zend_Gdata
  • 第37章 Zend_Http
  • 第38章 Zend_InfoCard
  • 第39章 Zend_Json
  • 第40章 Zend_Layout
  • 第41章 Zend_Ldap
  • 第42章 Zend_Loader
  • 第43章 Zend_Locale
  • 第44章 Zend_Log
  • 第45章 Zend_Mail
  • 第46章 Zend_Markup
  • 第47章 Zend_Measure
  • 第48章 Zend_Memory
  • 第49章 Zend_Mime
  • 第50章 Zend_Navigation
  • 第51章 Zend_Oauth
  • 第52章 Zend_OpenId
  • 第53章 Zend_Paginator
  • 第54章 Zend_Pdf
  • 第55章 Zend_ProgressBar
  • 第56章 Zend_Queue
  • 第57章 Zend_Reflection
  • 第58章 Zend_Registry
  • 第59章 Zend_Rest

  • 第60章 Zend_Search_Lucene
  • 第61章 Zend_Serializer
  • 第62章 Zend_Server
  • 第63章 Zend_Service
  • 第64章 Zend_Session
  • 第65章 Zend_Soap
  • 第66章 Zend_Tag
  • 第67章 Zend_Test
  • 第68章 Zend_Text
  • 第69章 Zend_TimeSync
  • 第70章 Zend_Tool
  • 第71章 Zend_Tool_Framework
  • 第72章 Zend_Tool_Project
  • 第73章 Zend_Translate
  • 第74章 Zend_Uri
  • 第75章 Zend_Validate
  • 第76章 Zend_Version
  • 第77章 Zend_View
  • 第78章 Zend_Wildfire
  • 第79章 Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Translation 70.6% Update 2010-11-28 - Revision 23415

    7.2. Basic Placeholder Usage

    Zend Framework defines a generic placeholder() view helper that you may use for as many custom placeholders you need. It also provides a variety of specific placeholder implementations for often-needed functionality, such as specifying the DocType declaration, document title, and more.

    All placeholders operate in roughly the same way. They are containers, and thus allow you to operate on them as collections. With them you can:

    • Append or prepend items to the collection.

    • Replace the entire collection with a single value.

    • Specify a string with which to prepend output of the collection when rendering.

    • Specify a string with which to append output of the collection when rendering.

    • Specify a string with which to separate items of the collection when rendering.

    • Capture content into the collection.

    • Render the aggregated content.

    Typically, you will call the helper with no arguments, which will return a container on which you may operate. You will then either echo this container to render it, or call methods on it to configure or populate it. If the container is empty, rendering it will simply return an empty string; otherwise, the content will be aggregated according to the rules by which you configure it.

    As an example, let's create a sidebar that consists of a number of "blocks" of content. You'll likely know up-front the structure of each block; let's assume for this example that it might look like this:

    <div class="sidebar">
        <
    div class="block">
            <
    p>
                
    Lorem ipsum dolor sit ametconsectetur adipiscing elitVivamus
                consectetur aliquet odio ac consectetur
    Nulla quis eleifend
                tortor
    Pellentesque variusodio quis bibendum consequatdiam
                lectus porttitor quam
    et aliquet mauris orci eu augue.
            </
    p>
        </
    div>
        <
    div class="block">
            <
    ul>
                <
    li><a href="/some/target">Link</a></li>
                <
    li><a href="/some/target">Link</a></li>
            </
    ul>
        </
    div>
    </
    div>

    The content will vary based on the controller and action, but the structure will be the same. Let's first setup the sidebar in a resource method of our bootstrap:

    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
    {
        
    // ...

        
    protected function _initSidebar()
        {
            
    $this->bootstrap('View');
            
    $view $this->getResource('View');

            
    $view->placeholder('sidebar')
                 
    // "prefix" -> markup to emit once before all items in collection
                 
    ->setPrefix("<div class=\"sidebar\">\n    <div class=\"block\">\n")
                 
    // "separator" -> markup to emit between items in a collection
                 
    ->setSeparator("</div>\n    <div class=\"block\">\n")
                 
    // "postfix" -> markup to emit once after all items in a collection
                 
    ->setPostfix("</div>\n</div>");
        }

        
    // ...
    }

    The above defines a placeholder, "sidebar", that has no items. It configures the basic markup structure of that placeholder, however, per our requirements.

    Now, let's assume for the "user" controller that for all actions we'll want a block at the top containing some information. We could accomplish this in two ways: (a) we could add the content to the placeholder directly in the controller's preDispatch() method, or (b) we could render a view script from within the preDispatch() method. We'll use (b), as it follows a more proper separation of concerns (leaving view-related logic and functionality within a view script).

    We'll name the view script "user/_sidebar.phtml", and populate it as follows:

    <?php $this->placeholder('sidebar')->captureStart() ?>
    <h4>User Administration</h4>
    <ul>
        <li><a href="<?php $this->url(array('action' => 'list')) ?>">
            List</a></li>
        <li><a href="<?php $this->url(array('action' => 'create')) ?>">
            Create</a></a></li>
    </ul>
    <?php $this->placeholder('sidebar')->captureEnd() ?>

    The above example makes use of the content capturing feature of placeholders. By default, content is appended as a new item in the container, allowing us to aggregate content. This example makes use of view helpers and static HTML in order to generate markup, and the content is then captured and appended into the placeholder itself.

    To invoke the above view script, we would write the following in our preDispatch() method:

    class UserController extends Zend_Controller_Action
    {
        
    // ...

        
    public function preDispatch()
        {
            
    // ...

            
    $this->view->render('user/_sidebar.phtml');

            
    // ...
        
    }

        
    // ...
    }

    Note that we're not capturing the rendered value; there's no need, as the entierty of that view is being captured into a placeholder.

    Now, let's assume our "view" action in that same controller needs to present some information. Within the "user/view.phtml" view script, we might have the following snippet of content:

    $this->placeholder('sidebar')
         ->
    append('<p>User: ' $this->escape($this->username) .  '</p>');

    This example makes use of the append() method, and passes it some simple markup to aggregate.

    Finally, let's modify our layout view script, and have it render the placeholder.

    <html>
    <
    head>
        <
    title>My Site</title>
    </
    head>
    <
    body>
        <
    div class="content">
            <?
    php echo $this->layout()->content ?>
        </div>
        <?php echo $this->placeholder('sidebar'?>
    </body>
    </html>

    For controllers and actions that do not populate the "sidebar" placeholder, no content will be rendered; for those that do, however, echoing the placeholder will render the content according to the rules we created in our bootstrap, and the content we aggregated throughout the application. In the case of the "/user/view" action, and assuming a username of "matthew", we would get content for the sidebar as follows (formatted for readability):

    <div class="sidebar">
        <
    div class="block">
            <
    h4>User Administration</h4>
            <
    ul>
                <
    li><a href="/user/list">List</a></li>
                <
    li><a href="/user/create">Create</a></a></li>
            </
    ul>
        </
    div>
        <
    div class="block">
            <
    p>Usermatthew</p>
        </
    div>
    </
    div>

    There are a large number of things you can do by combining placeholders and layout scripts; experiment with them, and read the relevant manual sections for more information.

    digg delicious meneame google twitter technorati facebook

    Comments

    Loading...