6.3 Понимание и использование Zend Form Decorators. Слои декораторов

Zend, “Understanding and Using Zend Form Decorators. Layering Decorators”, public translation into Russian from English More about this translation.

See also 44 similar translations

Translate into another language.

Participants

antdmi 9 points
Join Translated.by to translate! If you already have a Translated.by account, please sign in.
If you do not want to register an account, you can sign in with OpenID.
Pages: ← previous Ctrl next next untranslated
1 2 3 4 5

Understanding and Using Zend Form Decorators. Layering Decorators

6.3 Понимание и использование Zend Form Decorators. Слои декораторов

History of edits (Latest: antdmi 6 years, 9 months ago) §

If you were following closely in the previous section, you may have noticed that a 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 section. Recall:

  01. class My_Decorator_SimpleInput extends Zend_Form_Decorator_Abstract

  02. {

  03.     protected $_format = '<label for="%s">%s</label>'

  04.                      . '<input id="%s" name="%s" type="text" value="%s"/>';

Pages: ← previous Ctrl next next untranslated
1 2 3 4 5