Introduction to Zend Framework

 Learning Zend Framework

appendix

 Zend Framework Reference


  •  Zend_Gdata
  •  Zend_Http
  •  Zend_InfoCard
  •  Zend_Json
  •  Zend_Layout
  •  Zend_Ldap
  •  Zend_Loader
  •  Zend_Locale
  •  Zend_Log
  •  Zend_Mail
  •  Zend_Markup
  •  Zend_Measure
  •  Zend_Memory
  •  Zend_Mime
  •  Zend_Navigation
  •  Zend_Oauth
  •  Zend_OpenId
  •  Zend_Paginator
  •  Zend_Pdf
  •  Zend_ProgressBar
  •  Zend_Queue
  •  Zend_Reflection
  •  Zend_Registry
  •  Zend_Rest

  •  Zend_Search_Lucene
  •  Zend_Serializer
  •  Zend_Server
  •  Zend_Service
  •  Zend_Session
  •  Zend_Soap
  •  Zend_Tag
  •  Zend_Test
  •  Zend_Text
  •  Zend_TimeSync
  •  Zend_Tool
  •  Zend_Tool_Framework
  •  Zend_Tool_Project
  •  Zend_Translate
  •  Zend_Uri
  •  Zend_Validate
  •  Zend_Version
  •  Zend_View
  •  Zend_Wildfire
  •  Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Update 2011-11-16 - Revision 24438 - Version ZF 1.11.x

    Chapter 77. Zend_View

    Table of Contents

    77.1. Introduction
    77.1.1. Controller Script
    77.1.2. View Script
    77.1.3. Options
    77.1.4. Short Tags with View Scripts
    77.1.5. Utility Accessors
    77.2. Controller Scripts
    77.2.1. Assigning Variables
    77.2.2. Rendering a View Script
    77.2.3. View Script Paths
    77.3. View Scripts
    77.3.1. Escaping Output
    77.3.2. Using Alternate Template Systems
    77.3.2.1. Template Systems Using View Scripts
    77.3.2.2. Template Systems Using Zend_View_Interface
    77.4. View Helpers
    77.4.1. Initial Helpers
    77.4.1.1. Action View Helper
    77.4.1.2. BaseUrl Helper
    77.4.1.3. Currency Helper
    77.4.1.4. Cycle Helper
    77.4.1.5. Partial Helper
    77.4.1.6. Placeholder Helper
    77.4.1.6.1. Concrete Placeholder Implementations
    77.4.1.7. Doctype Helper
    77.4.1.8. Gravatar View Helper
    77.4.1.8.1. Options
    77.4.1.9. HeadLink Helper
    77.4.1.10. HeadMeta Helper
    77.4.1.11. HeadScript Helper
    77.4.1.12. HeadStyle Helper
    77.4.1.13. HeadTitle Helper
    77.4.1.14. HTML Object Helpers
    77.4.1.15. InlineScript Helper
    77.4.1.16. JSON Helper
    77.4.1.17. Navigation Helpers
    77.4.1.17.1. Translation of labels and titles
    77.4.1.17.2. Integration with ACL
    77.4.1.17.3. Navigation setup used in examples
    77.4.1.17.4. Breadcrumbs Helper
    77.4.1.17.5. Links Helper
    77.4.1.17.6. Menu Helper
    77.4.1.17.7. Sitemap Helper
    77.4.1.17.8. Navigation Helper
    77.4.1.18. TinySrc Helper
    77.4.1.18.1. Overview
    77.4.1.18.2. Quick Start
    77.4.1.18.3. Configuration Options
    77.4.1.18.4. Available Methods
    77.4.1.18.5. Examples
    77.4.1.19. Translate Helper
    77.4.1.20. UserAgent View Helper
    77.4.1.20.1. Overview
    77.4.1.20.2. Quick Start
    77.4.1.20.3. Available Methods
    77.4.2. Helper Paths
    77.4.3. Writing Custom Helpers
    77.4.4. Registering Concrete Helpers
    77.5. Zend_View_Abstract

    77.1. Introduction

    Zend_View is a class for working with the "view" portion of the model-view-controller pattern. That is, it exists to help keep the view script separate from the model and controller scripts. It provides a system of helpers, output filters, and variable escaping.

    Zend_View is template system agnostic; you may use PHP as your template language, or create instances of other template systems and manipulate them within your view script.

    Essentially, using Zend_View happens in two major steps: 1. Your controller script creates an instance of Zend_View and assigns variables to that instance. 2. The controller tells the Zend_View to render a particular view, thereby handing control over the view script, which generates the view output.

    77.1.1. Controller Script

    As a simple example, let us say your controller has a list of book data that it wants to have rendered by a view. The controller script might look something like this:

    // use a model to get the data for book authors and titles.
    $data = array(
        array(
            
    'author' => 'Hernando de Soto',
            
    'title' => 'The Mystery of Capitalism'
        
    ),
        array(
            
    'author' => 'Henry Hazlitt',
            
    'title' => 'Economics in One Lesson'
        
    ),
        array(
            
    'author' => 'Milton Friedman',
            
    'title' => 'Free to Choose'
        
    )
    );

    // now assign the book data to a Zend_View instance
    Zend_Loader::loadClass('Zend_View');
    $view = new Zend_View();
    $view->books $data;

    // and render a view script called "booklist.php"
    echo $view->render('booklist.php');

    77.1.2. View Script

    Now we need the associated view script, "booklist.php". This is a PHP script like any other, with one exception: it executes inside the scope of the Zend_View instance, which means that references to $this point to the Zend_View instance properties and methods. (Variables assigned to the instance by the controller are public properties of the Zend_View instance). Thus, a very basic view script could look like this:

     if ($this->books): ?>

        <!-- A table of some books. -->
        <table>
            <tr>
                <th>Author</th>
                <th>Title</th>
            </tr>

            <?php foreach ($this->books as $key => $val): ?>
            <tr>
                <td><?php echo $this->escape($val['author']) ?></td>
                <td><?php echo $this->escape($val['title']) ?></td>
            </tr>
            <?php endforeach; ?>

        </table>

    <?php else: ?>

        <p>There are no books to display.</p>

    <?php endif;?>

    Note how we use the "escape()" method to apply output escaping to variables.

    77.1.3. Options

    Zend_View has several options that may be set to configure the behaviour of your view scripts.

    • basePath: indicate a base path from which to set the script, helper, and filter path. It assumes a directory structure of:

      base/path/
          
      helpers/
          
      filters/
          
      scripts/

      This may be set via setBasePath(), addBasePath(), or the basePath option to the constructor.

    • encoding: indicate the character encoding to use with htmlentities(), htmlspecialchars(), and other operations. Defaults to UTF-8. May be set via setEncoding() or the encoding option to the constructor.

    • escape: indicate a callback to be used by escape(). May be set via setEscape() or the escape option to the constructor.

    • filter: indicate a filter to use after rendering a view script. May be set via setFilter(), addFilter(), or the filter option to the constructor.

    • strictVars: force Zend_View to emit notices and warnings when uninitialized view variables are accessed. This may be set by calling strictVars(true) or passing the strictVars option to the constructor.

    77.1.4. Short Tags with View Scripts

    In our examples, we make use of PHP long tags: <?php. We also favor the use of alternate syntax for control structures. These are convenient shorthands to use when writing view scripts, as they make the constructs more terse, keep statements on single lines, and eliminate the need to hunt for brackets within HTML.

    In previous versions, we often recommended using short tags (<? and <?=), as they make the view scripts slightly less verbose. However, the default for the php.ini short_open_tag setting is typically off in production or on shared hosts -- making their use not terribly portable. If you use template XML in view scripts, short open tags will cause the templates to fail validation. Finally, if you use short tags when short_open_tag is off, the view scripts will either cause errors or simply echo PHP code back to the viewer.

    If, despite these warnings, you wish to use short tags but they are disabled, you have two options:

    • Turn on short tags in your .htaccess file:

      php_value "short_open_tag" "on"

      This will only be possible if you are allowed to create and utilize .htaccess files. This directive can also be added to your httpd.conf file.

    • Enable an optional stream wrapper to convert short tags to long tags on the fly:

      $view->setUseStreamWrapper(true);

      This registers Zend_View_Stream as a stream wrapper for view scripts, and will ensure that your code continues to work as if short tags were enabled.

    [Warning] View Stream Wrapper Degrades Performance

    Usage of the stream wrapper will degrade performance of your application, though actual benchmarks are unavailable to quantify the amount of degradation. We recommend that you either enable short tags, convert your scripts to use full tags, or have a good partial and/or full page content caching strategy in place.

    77.1.5. Utility Accessors

    Typically, you'll only ever need to call on assign(), render(), or one of the methods for setting/adding filter, helper, and script paths. However, if you wish to extend Zend_View yourself, or need access to some of its internals, a number of accessors exist:

    • getVars() will return all assigned variables.

    • clearVars() will clear all assigned variables; useful when you wish to re-use a view object, but want to control what variables are available.

    • getScriptPath($script) will retrieve the resolved path to a given view script.

    • getScriptPaths() will retrieve all registered script paths.

    • getHelperPath($helper) will retrieve the resolved path to the named helper class.

    • getHelperPaths() will retrieve all registered helper paths.

    • getFilterPath($filter) will retrieve the resolved path to the named filter class.

    • getFilterPaths() will retrieve all registered filter paths.

    digg delicious meneame google twitter technorati facebook

    Comments

    Loading...