layout-intro.xml 2.2 KB

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