PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Introduction

When building a website using Zend Framework MVC layers, your view scripts will typically be just snippets of HTML pertinent to the requested action. For instance, if you had the action "/user/list", you might create a view script that iterates through the users and presents an unordered list:


<h2>Users</h2>
<ul>
    <?php if (!count($this->users)): ?>
    <li>No users found</li>
    <?php else: ?>
    <?php foreach ($this->users as $user): ?>
    <li>
        <?php echo $this->escape($user->fullname?>
        (<?php echo $this->escape($user->email?>)
    </li>
    <?php endforeach ?>
    <?php endif ?>
</ul>

Since this is just a snippet of HTML, it's not a valid page; it's missing a DOCTYPE declaration, and the opening HTML and BODY tags. So, the question is, where will these be created?

In early versions of Zend Framework, developers often created "header" and "footer" view scripts that had these artifacts, and then in each view script they would render them. While this methodology worked, it also made it difficult to refactor later, or to build composite content by calling multiple actions.

The Two Step View design pattern answers many of the issues presented. In this pattern, the "application" view is created first, and then injected into the "page" view, which is then presented to the client. The page view can be thought of as your site-wide template or layout, and would have common elements used across various pages.

Within Zend Framework, Zend_Layout implements the Two Step View pattern.

Zend Framework