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-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
23
* @see Zend_Form_Decorator_Abstract
25
require_once 'Zend/Form/Decorator/Abstract.php';
28
* Zend_Form_Decorator_Element_HtmlTag
30
* Wraps content in an HTML block tag.
32
* Options accepted are:
33
* - tag: tag to use in decorator
34
* - noAttribs: do not render attributes in the opening tag
35
* - placement: 'append' or 'prepend'. If 'append', renders opening and
36
* closing tag after content; if prepend, renders opening and closing tag
38
* - openOnly: render opening tag only
39
* - closeOnly: render closing tag only
41
* Any other options passed are processed as HTML attributes of the tag.
45
* @subpackage Decorator
46
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
47
* @license http://framework.zend.com/license/new-bsd New BSD License
48
* @version $Id: HtmlTag.php 12514 2008-11-10 16:30:24Z matthew $
50
class Zend_Form_Decorator_HtmlTag extends Zend_Form_Decorator_Abstract
53
* Placement; default to surround content
56
protected $_placement = null;
67
protected $_tagFilter;
70
* Convert options to tag attributes
74
protected function _htmlAttribs(array $attribs)
77
foreach ((array) $attribs as $key => $val) {
78
$key = htmlspecialchars($key, ENT_COMPAT, 'UTF-8');
80
$val = implode(' ', $val);
82
$val = htmlspecialchars($val, ENT_COMPAT, 'UTF-8');
83
$xhtml .= " $key=\"$val\"";
91
* Ensures tag is alphanumeric characters only, and all lowercase.
96
public function normalizeTag($tag)
98
if (!isset($this->_tagFilter)) {
99
require_once 'Zend/Filter.php';
100
require_once 'Zend/Filter/Alnum.php';
101
require_once 'Zend/Filter/StringToLower.php';
102
$this->_tagFilter = new Zend_Filter();
103
$this->_tagFilter->addFilter(new Zend_Filter_Alnum())
104
->addFilter(new Zend_Filter_StringToLower());
106
return $this->_tagFilter->filter($tag);
113
* @return Zend_Form_Decorator_HtmlTag
115
public function setTag($tag)
117
$this->_tag = $this->normalizeTag($tag);
124
* If no tag is registered, either via setTag() or as an option, uses 'div'.
128
public function getTag()
130
if (null === $this->_tag) {
131
if (null === ($tag = $this->getOption('tag'))) {
132
$this->setTag('div');
135
$this->removeOption('tag');
143
* Get the formatted open tag
146
* @param array $attribs
149
protected function _getOpenTag($tag, array $attribs = null)
152
if (null !== $attribs) {
153
$html .= $this->_htmlAttribs($attribs);
160
* Get formatted closing tag
165
protected function _getCloseTag($tag)
167
return '</' . $tag . '>';
171
* Render content wrapped in an HTML tag
173
* @param string $content
176
public function render($content)
178
$tag = $this->getTag();
179
$placement = $this->getPlacement();
180
$noAttribs = $this->getOption('noAttribs');
181
$openOnly = $this->getOption('openOnly');
182
$closeOnly = $this->getOption('closeOnly');
183
$this->removeOption('noAttribs');
184
$this->removeOption('openOnly');
185
$this->removeOption('closeOnly');
189
$attribs = $this->getOptions();
192
switch ($placement) {
195
return $content . $this->_getCloseTag($tag);
198
return $content . $this->_getOpenTag($tag, $attribs);
201
. $this->_getOpenTag($tag, $attribs)
202
. $this->_getCloseTag($tag);
205
return $this->_getCloseTag($tag) . $content;
208
return $this->_getOpenTag($tag, $attribs) . $content;
210
return $this->_getOpenTag($tag, $attribs)
211
. $this->_getCloseTag($tag)
214
return (($openOnly || !$closeOnly) ? $this->_getOpenTag($tag, $attribs) : '')
216
. (($closeOnly || !$openOnly) ? $this->_getCloseTag($tag) : '');