php – Zend Framework Zend_Form装饰器:里面的按钮元素?
我有一个像我这样创建的按钮元素:
$submit = new Zend_Form_Element_Button('submit'); $submit->setLabel('My Button'); $submit->setDecorators(array( 'ViewHelper',array('HtmlTag',array('tag' => 'li')) )); $submit->setAttrib('type','submit'); 这会生成以下HTML: <li> <label for="submit" class="optional">My Button</label> <button name="submit" id="submit" type="submit">My Button</button> </li> 我想用< span>包裹按钮的内部,如下所示: <button...><span>My Button</span></button> 使用Zend_Form执行此操作的最佳方法是什么?
我已经尝试过,并且最终使用相同的方法无法实现这一目标.似乎最简单的方法是:
... $submit->setLabel('<span>My Button</span>'); ... 但是,跨度将被转义.完全可以关闭标签装饰器的转义,但是,添加标签装饰器会导致输出错误,例如: $decorator = array( array('ViewHelper'),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中.也就是说,这是一个开始,它解决了你的问题. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |