我已经尝试过,并最终未能通过相同的方法达到这个目的。看起来最容易的方法是:
...
$submit->setLabel('<span>My Button</span>');
...
但是,跨度将被转义。这是完全可能的。然而,关闭逸出拉布勒装饰的,添加标签装饰渲染输出错误,例如:
$decorator = array(
array('ViewHelper'),
array('HtmlTag', array('tag' => 'li')),
array('Label', array('escape' => false))
);
$submit = new Zend_Form_Element_Button('submit');
$submit->setLabel('<span>My Button</span>');
$submit->setDecorators($decorator);
$submit->setAttrib('type', 'submit');
...呈现:
<label for="submit" class="optional"><span>My Button</span></label>
<li>
<button name="submit" id="submit" type="submit"><span>My Button</span></button>
</li>
...这,除了语义上不正确(易于修复)之外,仍然在转义元素内的span标签。
那么你会怎么做?
那么我认为最好的方法(这是我的元建议,当严格控制Zend_Form渲染)是使用ViewScript修饰器。
$submit = new Zend_Form_Element_Button('submit');
$submit->setLabel('My Button');
$submit->setDecorators(array(array('ViewScript', array('viewScript' => '_submitButton.phtml'))));
$submit->setAttrib('type', 'submit');
...然后在_submitButton.phtml定义如下:
<li>
<?= $this->formLabel($this->element->getName(), $this->element->getLabel()); ?>
<button
<?php
$attribs = $this->element->getAttribs();
echo
' name="' . $this->escape($this->element->getName()) . '"' .
' id="' . $this->escape($this->element->getId()) . '"' .
' type="' . $this->escape($attribs['type']) . '"';
?>
<?php
$value = $this->element->getValue();
if(!empty($value))
{
echo ' value="' . $this->escape($this->element->getValue()) . '"';
}
?>
>
<span>
<?= $this->escape($this->element->getLabel()); ?>
</span>
</button>
</li>
的_submitButton.phtml文件将需要在视图脚本目录(你可能是最好的加一个特定的表单装饰器使用$view->addScriptPath('/path/to/my/form/decorators')
)。
这应该呈现你正在寻找的东西。由于我在工作中遇到的灵活性问题,我刚刚开始查看ViewScript装饰器。你会注意到我的脚本不是那么灵活,并且肯定不在BNF中,因为所有可以在元素对象上填充的成员。这就是说,这是一个开始,它解决了你的问题。
谢谢你的详细回复。我知道ViewScript装饰器,但我不知道它可以应用于单个元素。 – leek 2009-05-02 05:05:37