layout-intro.xml 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="learning.layout.intro">
  4. <title>Introduction</title>
  5. <para>
  6. When building a website using the Zend Framework <acronym>MVC</acronym> layers, your view
  7. scripts will typically be just snippets of <acronym>HTML</acronym> pertinent to the
  8. requested action. For instance, if you had the action "/user/list", you might create a view
  9. script that iterates through the users and presents an unordered list:
  10. </para>
  11. <programlisting language="php"><![CDATA[
  12. <h2>Users</h2>
  13. <ul>
  14. <?php if (!count($this->users)): ?>
  15. <li>No users found</li>
  16. <?php else: ?>
  17. <?php foreach ($this->users as $user): ?>
  18. <li>
  19. <?php echo $this->escape($user->fullname) ?>
  20. (<?php echo $this->escape($user->email) ?>)
  21. </li>
  22. <?php endforeach ?>
  23. <?php endif ?>
  24. </ul>
  25. ]]></programlisting>
  26. <para>
  27. Since this is just a snippet of <acronym>HTML</acronym>, it's not a valid page; it's missing
  28. a <acronym>Doctype</acronym> declaration, and the opening <acronym>HTML</acronym> and
  29. <acronym>body</acronym> tags. So, the question is, where will these be created?
  30. </para>
  31. <para>
  32. In early versions of Zend Framework, developers often created "header" and "footer" view
  33. scripts that had these artifacts, and then in each view script they would render them. While
  34. this methodology worked, it also made it difficult to refactor later, or to build composite
  35. content by calling multiple actions.
  36. </para>
  37. <para>
  38. The <ulink url="http://martinfowler.com/eaaCatalog/twoStepView.html">Two Step View</ulink>
  39. design pattern answers many of the issues presented. In this pattern, the "application" view
  40. is created first, and then injected into the "page" view, which is then presented to the
  41. client. The page view can be thought of as your site-wide template or layout, and would have
  42. common elements used across various pages.
  43. </para>
  44. <para>
  45. Within Zend Framework, <classname>Zend_Layout</classname> implements the Two Step View
  46. pattern.
  47. </para>
  48. </sect1>