LogoPhly, boy, phly
the weblog and site of Matthew Weier O'Phinney

Wednesday, December 19. 2007

Zend_Layout and Zend_View Enhanced components now in core

I'm pleased to announce that the Zend_View Enhanced and Zend_Layout components are now in the Zend Framework core. With these two components, you can now create some truly complex views for your application with relative ease.

The two components tackle several view related tasks:

  • Layouts, or Two Step Views
  • Partials (view fragment scripts with their own variable scope)
  • Placeholders (store data and/or markup for later retrieval)
  • Actions (dispatch a controller action)

So, what's the big deal? Much, if not all of this, was already possible, I hear some people saying. Well, yes, technically it was; in fact, all of these, except layouts, were accomplished by the addition of extra view helpers, which anybody could have written (and, in fact, some did). However, by having these as a standard part of the library, there are now standard ways to perform these tasks -- meaning consistency between applications.

Plus, these helpers just make things so much simpler!

For instance, who out there has all the DOCTYPE declarations memorized? I personally know all the types, but can't rattle off the entire declarations associated with each to save my life. With the doctype() helper, all I have to do is:


<?= $this->doctype('XHTML1_TRANSITIONAL') ?>
 

and it's now present. Furthermore, by putting this at the top of my layout, when I display my scripts as aggregated in the headScript() helper, they'll now be properly escaped as XML CDATA, as helpers that need to be DOCTYPE aware now determine this information from that helper.

Speaking of the headScript() helper, it's pretty handy. Let's say you have an application that requires javascript. Instead of unconditionally specifying the javascript include for every controller, or setting up complex logic for determining when to include it, you can have your application view specify it's needed:


<?php $this->headScript()->appendFile('/js/foo.js') ?>
 

Then, in your master layout script, you tell it to include any scripts aggregated:


<?= $this->headScript() ?>
 

You can do similarly for specifying feeds (via headLink()), stylesheets (via headLink() for external files, headStyle() for inline stylesheets), and even your title element (for instance, you could aggregate your various breadcrumbs, and then specify a custom separator to use between them).

This is really just the tip of the iceberg. Using a combination of placeholders, partials, actions, and normal view helpers, you can then create some pretty complex layouts using minimal markup. As an example:


<?= $this->doctype('XHTML1_TRANSITIONAL') ?>
<html>
    <head>
        <?= $this->headTitle() ?>
        <?= $this->headMeta()->setIndent(8) ?>
        <?= $this->headLink()->setIndent(8) ?>
        <?= $this->headStyle()->setIndent(8) ?>
        <?= $this->headScript()->setIndent(8) ?>
    </head>
    <body>
        <?= $this->partial('header.phtml') ?>
        <div id="content">
            <?= $this->layout()->content ?>
        </div>
        <?= $this->subnav() ?>
        <?= $this->partial('footer.phtml') ?>
        <?= $this->inlineScript() ?>
    </body>
</html>
 

The example above makes use of several placeholders (doctype, HeadTitle, HeadMeta, HeadLink, HeadStyle, HeadScript, and InlineScript), two partials (for the header and footer), layout content, and a custom view helper (for navigation); the entire thing is less than 20 lines long, yet contains everything necessary for your site layout.

The functionality of these new components is not only broad, but deep as well, and can't be covered in a single blog post. Look for a series of tutorials on the Zend Developer Zone detailing them in the coming weeks. In the meantime, you can read the documentation available in the ZF subversion repository.

Posted by Matthew Weier O'Phinney in PHP at 08:44 | Comments (3) | Trackbacks (2)
Defined tags for this entry: mvc, php, zend framewor
Related entries by tags:
Server Upgrades... lost entries...
Form Decorators Tutorial posted
Zend Framework Q&A Session
View Helpers Tutorial on DevZone
ZF Plugins Tutorial on DevZone

Trackbacks
Trackback specific URI for this entry

Matthew O'Phinney's Blog: Zend_Layout and Zend_View Enhanced components now in core
Matthew O'Phinney notes today that the Zend_View_Enhanced and Zend_Layout components for ...
Weblog: PHPDeveloper.org
Tracked: Dec 20, 09:02
Nowo?ci w ZF: Zend_Layout i ulepszone Zend_View
Matthew Weier O’Phinney niespe?na miesi?c temu poinformowa? w swoim blogu, ?e do Zend Framework do??czono nowy komponent Zend_Layout oraz rozszerzono mo?liwo?ci Zend_View. Wydaj? si? to by? bardzo ciekawe ficzery, zatem przyjrzyjmy si...
Weblog: dominik bu?aj
Tracked: Jan 08, 01:53

Comments
Display comments as (Linear | Threaded)

DOCTYPE FTW! well, i was a zend framework fan, now an agavi fan but this is interesting and a very nice development thats for sure, much effort would be saved now.

next step, getting all the js/css files, creating 1 file and minimizing? :-)
#1 Trophaeum (Link) on 2007-12-19 10:39 (Reply)
I've actually written functionality for a site that gzips and minifies JS, caching it to disk to server as requested; it's not terribly difficult, but requires a dependency on a 3rd party library for the minification. It may make for a good addition to ZF... hadn't thought about it at the time. :-)
#1.1 Matthew Weier O'Phinney (Link) on 2007-12-19 10:49 (Reply)
I've been quiet on the lists recently but I do lurk waiting for the busy Autumn/Winter season at work to end so I can tinker, annoy and plague some more. Great to see things making it to Core! I'll have some fun on my next ZF project now all these wild ideas are standardised :-).

Now all I need to do is add ZF MVC support to PHPSpec, get some command line generators running, and my development life will be golden.
#1.1.1 P (Link) on 2007-12-19 12:24 (Reply)

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA 1CAPTCHA 2CAPTCHA 3CAPTCHA 4CAPTCHA 5


 
 
  • Home
  • Resume
  • Blog
  • Phly PEAR Channel
  • Contact Me
  • About this site

ZCE

Zend Education Advisory Board Member

Add to Technorati Favorites

Calendar

Back May '08 Forward
Mon Tue Wed Thu Fri Sat Sun
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

Quicksearch

Links

  • PHLY - PHp LibrarY
  • Paul M. Jones
  • Mike Naberezny
  • Shahar Evron
  • Planet PHP
  • Zend Where I now work
  • Garden.org Where I once worked

Archives

May 2008
April 2008
March 2008
Recent...
Older...

Categories

XML Linux
XML Personal
XML Aikido
XML Family
XML Programming
XML Perl
XML PHP

All categories

Syndicate This Blog

XML RSS 0.91 feed
XML RSS 1.0 feed
XML RSS 2.0 feed
ATOM/XML ATOM 0.3 feed
ATOM/XML ATOM 1.0 feed
XML RSS 2.0 Comments

Show tagged entries

xml best practices
xml books
xml conferences
xml file_fortune
xml linux
xml mvc
xml pear
xml personal
xml php
xml programming
xml ubuntu
xml webinar
xml zendcon
xml zend framework
© 2004 - present, Matthew Weier O'Phinney
matthew-web <at> weierophinney.net