7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
17 * @subpackage Decorator
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
22 /** Zend_Form_Decorator_Abstract */
23 require_once 'Zend/Form/Decorator/Abstract.php';
26 * Zend_Form_Decorator_ViewScript
28 * Render a view script as a decorator
30 * Accepts the options:
31 * - separator: separator to use between view script content and provided content (defaults to PHP_EOL)
32 * - placement: whether to append or prepend view script content to provided content (defaults to prepend)
33 * - viewScript: view script to use
35 * The view script is rendered as a partial; the element being decorated is
36 * passed in as the 'element' variable:
39 * echo $this->element->getLabel();
42 * Any options other than separator, placement, and viewScript are passed to
43 * the partial as local variables.
47 * @subpackage Decorator
48 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
49 * @license http://framework.zend.com/license/new-bsd New BSD License
50 * @version $Id: ViewScript.php 16218 2009-06-21 19:44:04Z thomas $
52 class Zend_Form_Decorator_ViewScript
extends Zend_Form_Decorator_Abstract
55 * Default placement: append
58 protected $_placement = 'APPEND';
61 * View script to render
64 protected $_viewScript;
69 * @param string $script
70 * @return Zend_Form_Decorator_ViewScript
72 public function setViewScript($script)
74 $this->_viewScript
= (string) $script;
83 public function getViewScript()
85 if (null === $this->_viewScript
) {
86 if (null !== ($element = $this->getElement())) {
87 if (null !== ($viewScript = $element->getAttrib('viewScript'))) {
88 $this->setViewScript($viewScript);
93 if (null !== ($viewScript = $this->getOption('viewScript'))) {
94 $this->setViewScript($viewScript)
95 ->removeOption('viewScript');
99 return $this->_viewScript
;
103 * Render a view script
105 * @param string $content
108 public function render($content)
110 $element = $this->getElement();
111 $view = $element->getView();
112 if (null === $view) {
116 $viewScript = $this->getViewScript();
117 if (empty($viewScript)) {
118 require_once 'Zend/Form/Exception.php';
119 throw new Zend_Form_Exception('No view script registered with ViewScript decorator');
122 $separator = $this->getSeparator();
123 $placement = $this->getPlacement();
125 $vars = $this->getOptions();
126 $vars['element'] = $element;
127 $vars['content'] = $content;
128 $vars['decorator'] = $this;
130 $renderedContent = $view->partial($viewScript, $vars);
132 // Get placement again to see if it has changed
133 $placement = $this->getPlacement();
135 switch ($placement) {
137 return $renderedContent . $separator . $content;
139 return $content . $separator . $renderedContent;
141 return $renderedContent;