Ein Layout erstellen
Wie man festgestellen kann waren die View Skripte im vorhergehenden Kapitel HTML Fragmente-
und keine kompletten Seiten. Das ist so gewünscht; wir wollen das unsere Aktionen nur den
Inhalt zurückgeben der für die Aktion selbst relevant ist, und nicht die Anwendung als
ganzes.
Jetzt müssen wir den erstellten Inhalt zu einer kompletten HTML Seite zusammenfügen. Wir
wollen auch ein konsistentes Aussehen und Feeling für die Anwendung haben. Wir wollen ein
globales Sitelayout verwenden um beide Arbeiten zu ermöglichen.
Es gibt zwei Design Pattern die Zend Framework verwendet um Layouts zu implementieren:
Two Step View und
Composite
View. Two Step View wird normalerweise mit dem Transform View
Pattern assoziiert; die grundsätzliche Idee besteht darin das die View der Anwendung eine
Repräsentation erstellt die dann in die Master View für letzte Transformationen injiziert
wird. Das Composite View Pattern arbeitet mit einer View die aus ein
oder mehreren atomischen Anwendungs Views gemacht ist.
Im Zend Framework kombiniert Zend_Layout die Ideen hinter
diesen Pattern. Statt dass jedes Action View Skript Site-weite Artefakte einfügen muss,
können Sie sich einfach auf Ihre eigenen Beantwortungen konzentrieren.
Natürlich benötigt man trotzdem Anwendungs-spezifische Informationen im Site-weiten View
Skript. Glücklicherweise bietet Zend Framework eine Anzahl von View
Platzhaltern die es erlauben solche Informationen von den Action View
Skripten zu bekommen.
To get started using Zend_Layout, first we need to inform our bootstrap to use the
Layout resource. This can be done by adding the following line to
your application/configs/application.ini file, within the
production section:
The final INI file should look as follows:
This directive tells your application to look for layout view scripts in
application/layouts/scripts. Create those directories now.
We also want to ensure we have an XHTML DocType declaration for our application. To enable
this, we need to add a resource to our bootstrap.
The simplest way to add a bootstrap resource is to simply create a protected method
beginning with the phrase _init. In this case, we want to
initialize the doctype, so we'll create an _initDoctype() method
within our bootstrap class:
Within that method, we need to hint to the view to use the appropriate doctype. But where
will the view object come from? The easy solution is to initialize the
View resource; once we have, we can pull the view object from the
bootstrap and use it.
To initialize the view resource, add the following line to your
application/configs/application.ini file, in the section marked
production:
This tells us to initialize the view with no options (the '[]' indicates that the "view" key
is an array, and we pass nothing to it).
Now that we have a view, let's flesh out our _initDoctype() method.
In it, we will first ensure the View resource has run, fetch the view
object, and then configure it:
bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
}
}
]]>
Now that we've initialized Zend_Layout and set the Doctype, let's
create our site-wide layout:
doctype() ?>
Zend Framework Quickstart Application
headLink()->appendStylesheet('/css/global.css') ?>
layout()->content ?>
]]>
We grab our application content using the layout() view helper, and
accessing the "content" key. You may render to other response segments if you wish to, but
in most cases, this is all that's necessary.
Note also the use of the headLink() placeholder. This is an easy
way to generate the HTML for <link> elements, as well as to keep track of them
throughout your application. If you need to add additional CSS sheets to support a single
action, you can do so, and be assured it will be present in the final rendered page.
Checkpoint
Now go to "http://localhost" and check out the source. You should see your XHTML header,
head, title, and body sections.