6.4 Понимание и использование Zend Form Decorators. Визуализация отдельных декораторов

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

See also 44 similar translations

Translate into another language.

Participants

antdmi 10 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. Rendering Individual Decorators

6.4 Понимание и использование Zend Form Decorators. Визуализация отдельных декораторов

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

In the previous section, we looked at how you can combine decorators to create complex output. We noted that while you have a ton of flexibility with this approach, it also adds some complexity and overhead. In this section, we will examine how to render decorators individually in order to create custom markup for forms and/or individual elements.

Once you have registered your decorators, you can later retrieve them by name from the element. Let's review the previous example:

  01. $element = new Zend_Form_Element('foo', array(

  02.     'label'     => 'Foo',

  03.     'belongsTo' => 'bar',

  04.     'value'     => 'test',

  05.     'prefixPath' => array('decorator' => array(

  06.         'My_Decorator' => 'path/to/decorators/',

  07.     )),

  08.     'decorators' => array(

  09.         'SimpleInput'

  10.         array('SimpleLabel', array('placement' => 'append')),

  11.     ),

  12. ));

If we wanted to pull and render just the SimpleInput decorator, we can do so using the getDecorator() method:

  01. $decorator = $element->getDecorator('SimpleInput');

  02. echo $decorator->render('');

This is pretty easy, but it can be made even easier; let's do it in a single line:

  01. echo $element->getDecorator('SimpleInput')->render('');

Not too bad, but still a little complex. To make this easier, a shorthand notation was introduced into Zend_Form in 1.7: you can render any registered decorator by calling a method of the format renderDecoratorName(). This will effectively perform what you see above, but makes the $content argument optional and simplifies the usage:

  01. echo $element->renderSimpleInput();

This is a neat trick, but how and why would you use it?

Many developers and designers have very precise markup needs for their forms. They would rather have full control over the output than rely on a more automated solution which may or may not conform to their design. In other cases, the form layout may require a lot of specialized markup -- grouping arbitrary elements, making some invisible unless a particular link is selected, etc.

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