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

Monday, April 6. 2009

From the inside-out: How to layer decorators

This marks the second in an on-going series on Zend_Form decorators.

You may have noticed in the previous installment that the decorator's render() method takes a single argument, $content. This is expected to be a string. render() will then take this string and decide to either replace it, append to it, or prepend it. This allows you to have a chain of decorators -- which allows you to create decorators that render only a subset of the element's metadata, and then layer these decorators to build the full markup for the element.

Let's look at how this works in practice.

For most form element types, the following decorators are used:

  • ViewHelper (render the form input using one of the standard form view helpers)
  • Errors (render validation errors via an unordered list)
  • Description (render any description attached to the element; often used for tooltips)
  • HtmlTag (wrap all of the above in a <dd> tag
  • Label (render the label preceding the above, wrapped in a <dt> tag

You'll notice that each of these decorators does just one thing, and operates on one specific piece of metadata stored in the form element: the "Errors" decorator pulls validation errors and renders them; the "Label" decorator pulls just the label and renders it. This allows the individual decorators to be very succinct, repeatable, and, more importantly, testable.

It's also where that $content argument comes into play: each decorator's render() method is designed to accept content, and then either replace it (usually by wrapping it), prepend to it, or append to it.

So, it's best to think of the process of decoration as one of building an onion from the inside out.

To simplify the process, we'll take a look at the example from the previous entry in this series. Recall:


class My_Decorator_SimpleInput extends Zend_Form_Decorator_Abstract
{
    protected $_format = '<label for="%s">%s</label><input id="%s" name="%s" type="text" value="%s"/>';

    public function render($content)
    {
        $element = $this->getElement();
        $name    = htmlentities($element->getFullyQualifiedName());
        $label   = htmlentities($element->getLabel());
        $id      = htmlentities($element->getId());
        $value   = htmlentities($element->getValue());

        $markup  = sprintf($this->_format, $id, $label, $id, $name, $value);
        return $markup;
    }
}
 

Let's now remove the label functionality, and build a separate decorator for that.


class My_Decorator_SimpleInput extends Zend_Form_Decorator_Abstract
{
    protected $_format = '<input id="%s" name="%s" type="text" value="%s"/>';

    public function render($content)
    {
        $element = $this->getElement();
        $name    = htmlentities($element->getFullyQualifiedName());
        $id      = htmlentities($element->getId());
        $value   = htmlentities($element->getValue());

        $markup  = sprintf($this->_format, $id, $name, $value);
        return $markup;
    }
}

class My_Decorator_SimpleLabel extends Zend_Form_Decorator_Abstract
{
    protected $_format = '<label for="%s">%s</label>';

    public function render($content)
    {
        $element = $this->getElement();
        $id      = htmlentities($element->getId());
        $label   = htmlentities($element->getLabel());

        $markup = sprintf($this->_format, $id, $label);
        return $markup;
    }
}
 

Now, this may look all well and good, but here's the problem: as written currently, the last decorator to run wins, and overwrites everything. You'll end up with just the input, or just the label, depending on which you register last.

To overcome this, simply concatenate the passed in $content with the markup somehow:


return $content . $markup;
 

The problem with the above approach comes when you want to programmatically choose whether the original content should precede or append the new markup. Fortunately, there's a standard mechanism for this already; Zend_Form_Decorator_Abstract has a concept of placement and defines some constants for matching it. Additionally, it allows specifying a separator to place between the two. Let's make use of those:


class My_Decorator_SimpleInput extends Zend_Form_Decorator_Abstract
{
    protected $_format = '<input id="%s" name="%s" type="text" value="%s"/>';

    public function render($content)
    {
        $element = $this->getElement();
        $name    = htmlentities($element->getFullyQualifiedName());
        $id      = htmlentities($element->getId());
        $value   = htmlentities($element->getValue());

        $markup  = sprintf($this->_format, $id, $name, $value);

        $placement = $this->getPlacement();
        $separator = $this->getSeparator();
        switch ($placement) {
            case self::PREPEND:
                return $markup . $separator . $content;
            case self::APPEND:
            default:
                return $content . $separator . $markup;
        }
    }
}

class My_Decorator_SimpleLabel extends Zend_Form_Decorator_Abstract
{
    protected $_format = '<label for="%s">%s</label>';

    public function render($content)
    {
        $element = $this->getElement();
        $id      = htmlentities($element->getId());
        $label   = htmlentities($element->getLabel());

        $markup = sprintf($this->_format, $id, $label);

        $placement = $this->getPlacement();
        $separator = $this->getSeparator();
        switch ($placement) {
            case self::APPEND:
                return $content . $separator . $markup;
            case self::PREPEND:
            default:
                return $markup . $separator . $content;
        }
    }
}
 

Notice in the above that I'm switching the default case for each; the assumption will be that labels prepend content, and input appends.

Now, let's create a form element that uses these:


$element = new Zend_Form_Element('foo', array(
    'label'      => 'Foo',
    'belongsTo'  => 'bar',
    'value'      => 'test',
    'prefixPath' => array('decorator' => array(
        'My_Decorator' => 'path/to/decorators/',
    )),
    'decorators' => array(
        'SimpleInput',
        'SimpleLabel',
    ),
));
 

How will this work? When we call render(), the element will iterate through the various attached decorators, calling render() on each. It will pass an empty string to the very first, and then whatever content is created will be passed to the next, and so on:

  • Initial content is an empty string: ''
  • '' is passed to the SimpleInput decorator, which then generates a form input that it appends to the empty string: <input id="bar-foo" name="bar[foo]" type="text" value="test"/>
  • The input is then passed as content to the SimpleLabel decorator, which generates a label and prepends it to the original content; the default separator is a PHP_EOL character, giving us this: <label for="bar-foo">\n<input id="bar-foo" name="bar[foo]" type="text" value="test"/>

But wait a second! What if you wanted the label to come after the input for some reason? Remember that "placement" flag? You can pass it as an option to the decorator. The easiest way to do this is to pass an array of options with the decorator during element creation:


$element = new Zend_Form_Element('foo', array(
    'label'      => 'Foo',
    'belongsTo'  => 'bar',
    'value'      => 'test',
    'prefixPath' => array('decorator' => array(
        'My_Decorator' => 'path/to/decorators/',
    )),
    'decorators' => array(
        'SimpleInput',
        array('SimpleLabel', array('placement' => 'append')),
    ),
));
 

Notice that when passing options, you must wrap the decorator within an array; this hints to the constructor that options are available. The decorator name is the first element of the array, and options are passed in an array to the second element of the array.

The above results in the markup <input id="bar-foo" name="bar[foo]" type="text" value="test"/>\n<label for="bar-foo">.

Using this technique, you can have decorators that target specific metadata of the element or form and create only the markup relevant to that metadata; by using mulitiple decorators, you can then build up the complete element markup. Our onion is the result.

There are pros and cons to this approach. First, the cons:

  • More complex to implement. You have to pay careful attention to the decorators you use and what placement you utilize in order to build up the markup in the correct sequence.
  • More resource intensive. More decorators means more objects; multiply this by the number of elements you have in a form, and you may end up with some serious resource usage. Caching can help here.

The advantages are compelling, though:

  • Reusable decorators. You can create truly re-usable decorators with this technique, as you don't have to worry about the complete markup, but only markup for one or a few pieces of element/form metadata.
  • Ultimate flexibility. You can theoretically generate any markup combination you want from a small number of decorators.

While the above examples are the intended usage of decorators within Zend_Form, it's often hard to wrap your head around how the decorators interact with one another to build the final markup. For this reason, some flexibility was added in the 1.7 series to make rendering individual decorators possible -- which gives some Rails-like simplicity to rendering forms. Tune in to the next installment to see some of these techniques.

Updates

  • 2009-04-06 16:00-0500: updated append/prepend in SimpleLabel based on Mark's comment
  • 2009-04-07 08:50-0500: fixed typos in two examples, per mzeis
  • 2009-04-12 09:35-0500: fixed sprint to sprintf in two examples, per note from Joseph M.

Other articles in this series

  • The simplest Zend_Form decorator
Posted by Matthew Weier O'Phinney in PHP at 08:30 | Comments (19) | Trackbacks (0)
Defined tags for this entry: decorators, php, zend framework
Related entries by tags:
Autoloading Benchmarks
Applying FilterIterator to Directory Iteration
Running mod_php and FastCGI side-by-side
Creating Zend_Tool Providers
State of Zend Framework 2.0

Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)

Thanks for your mini-series.
It is so cool, it shows many things that are unclear after reading manual. This is really helpful!
#1 Dominik (Link) on 2009-04-06 11:52 (Reply)
I am going to bookmark this series. It is something I have been confused with in Zend Core for some time now.

However, I have a question about your code examples. When you switched the append and prepend constants for the SimpleLabel decorator, shouldn't you have swapped the code as well? It appears that for SimpleLable that prepend (and defautl) will stick the markup after the content instead of before it.
#2 Mark on 2009-04-06 15:41 (Reply)
Nice catch -- I've updated it now.
#2.1 Matthew Weier O'Phinney (Link) on 2009-04-06 16:10 (Reply)
There seems to be a lot of duplicate code in regards to the placement. Most of the Zend decorators end with:

switch ($placement) {
case self::APPEND:
return $content . $separator . $markup;
case self::PREPEND:
return $markup . $separator . $content;
}

Wouldn't it be a good idea to move this code out into either the abstract or a placement helper class?
#3 David (Link) on 2009-04-06 22:26 (Reply)
Probably. :-) You volunteering?
#3.1 Matthew Weier O'Phinney (Link) on 2009-04-06 22:41 (Reply)
Will have to sign one of thems CLA's....
Been interested in contributing for a while, but my account wasn't working (seems to be now). I used to only be able to access wiki pages while logged out.
#3.1.1 David (Link) on 2009-04-07 00:10 (Reply)
Hi Matthew, two small corrections for your code snippets:

First $element snippet: the semicolon is missing.
Second $element code block: the decorators-array is missing a comma.

But that's only for the nitpickings of us - keep on telling the world about the awesomeness of Zend_Form, it's a great series! ;-)
#4 mzeis on 2009-04-07 01:42 (Reply)
Fixes made -- thanks for the corrections!
#4.1 Matthew Weier O'Phinney (Link) on 2009-04-07 08:55 (Reply)
Thanks for that, like someone said, this is what I spent some time looking for in the documentation.
#5 Mikael on 2009-04-07 08:50 (Reply)
Your SimpleLabel has sprint and not sprintf
$markup = sprint($this->_format, $id, $label);
$markup = sprintf($this->_format, $id, $label);
#6 Joseph Montanez (Link) on 2009-04-12 04:11 (Reply)
Thanks -- I've updated the code now.
#6.1 Matthew Weier O'Phinney (Link) on 2009-04-12 09:41 (Reply)
Thank you Matt!
#7 Yaroslav Shatkevich on 2009-04-15 11:37 (Reply)
I still find this code:

$element = new Zend_Form_Element('foo', array(
'label' => 'Foo',
'belongsTo' => 'bar',
'value' => 'test',
'prefixPath' => array('decorator' => array(
'My_Decorator' => 'path/to/decorators/',
)),
'decorators' => array(
'SimpleInput',
array('SimpleLabel', array('placement' => 'append')),
),
));

confusing. Why are we passing in the names of the decorator objects as strings? Why not:

$element = new Zend_Form_Element('foo', array(
'label' => 'Foo',
'belongsTo' => 'bar',
'value' => 'test',
'decorators' => array(
new My_Decorators_SimpleInput(),
new My_Decorators_SimpleLabel( array('placement' => 'append')),
),
));

Doesn't this by-pass any checks the parser/compiler (yes, PHP has no compiler, but in theory) could have made? Spelling mistakes in the object names, changed parameters? How about the tokenizer and parse tree? It just feels weird.

I guess I am being dumb?

Nice set of articles, really useful :-) thank you

monk.e.boy
#8 monk.e.boy (Link) on 2009-04-28 12:02 (Reply)
There are two reasons, actually. First, there's no reason to instantiate objects that you may not use -- for instance, if you're displaying a form for the first time, why instantiate the validators? Lazy-loading is an important component of Zend_Form, as there can potentially be hundreds of objects in play for a given form (between elements, groups, validators, filters, and decorators).

Second, using the short names allows you to take advantage of the plugin loader -- which allows you to override existing plugins of the same name by specifying additional plugin paths on which to look. For instance, I may define a 'SimpleInput' decorator, and want to re-use the form on another project -- but use different markup. I could simply drop in another class that has the same class suffix ('SimpleInput'), and pass in the prefix path to the form class's constructor - and never need to change my form class definition at all.

Yes, you do open yourself to some potential problems -- but you'll also get exceptions if the plugin loader is unable to find the class. This sort of thing you can test for.
#8.1 Matthew Weier O'Phinney (Link) on 2009-04-28 12:10 (Reply)
Nice article, just one observation: you should use htmlspecialchars instead of htmlentities.
#9 Hodicska Gergely (Link) on 2009-05-03 13:47 (Reply)
Why? Wouldn't it be safer to use htmlentities?
#9.1 David (Link) on 2009-05-03 21:37 (Reply)
Thanks for this. It really helped me out!
#10 Faro (Link) on 2009-07-09 13:26 (Reply)
I found this very helpful, also. Now, we just need a "From the Inside Out" for Form Display Groups. :-) Having multiple display groups is difficult due to the use of a single 'setDisplayGroupDecorators'.

By the way, I have had a bit of difficulty getting prefixPath to work. Also, there are varriations from 1.6 to 1.7 to 1.9 with respect to paths. 1.6 is able to find classes without an "include 'file.php';". I believe that 1.7 requires an "include" to find the classes.
#11 David Mitchell on 2009-11-16 12:48 (Reply)
Actually, the principals I outlined here work for forms, display groups, and sub forms as well; the chief difference is that you always want to start with a 'FormElements' decorator to aggregate the elements of the form/group/subform object.

You can achieve multiple display groups with different decorators by simply passing different decorators to each.

Finally, the PluginLoader really hasn't changed much since 1.5 except to now allow testing for plugin existence via autoloading before doing a Zend_Loader::isReadable() call; otherwise, it's always used include_once under the hood. :-)
#11.1 Matthew Weier O'Phinney (Link) on 2009-11-16 13:30 (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

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

ZCE

Zend Education Advisory Board Member

Add to Technorati Favorites

Calendar

Back September '10
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      

Quicksearch

Links

  • PHLY - PHp LibrarY
  • Planet PHP
  • Zend Framework, where I'm project lead
  • Sebastian Bergmann
  • Cal Evans
  • Shahar Evron
  • Paul M. Jones
  • Bill Karwin
  • Mike Naberezny
  • Fabien Potencier
  • Ben Ramsey
  • Derick Rethans
  • Ralph Schindler
  • Marco Tabini

Archives

September 2010
August 2010
July 2010
Recent...
Older...

Categories

XML Linux
XML Personal
XML Aikido
XML Family
XML Programming
XML Dojo
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 apache
xml best practices
xml books
xml conferences
xml cw09
xml decorators
xml dojo
xml dpc08
xml file_fortune
xml git
xml linux
xml mvc
xml oop
xml pear
xml perl
xml personal
xml php
xml phpworks08
xml programming
xml rest
xml ubuntu
xml vim
xml webinar
xml zendcon
xml zendcon08
xml zendcon09
xml zend framework
© 2004 - present, Matthew Weier O'Phinney
matthew-web <at> weierophinney.net