我有一个像我这样创建的按钮元素:
$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>
我想用&lt; span&gt;包裹按钮内部,如下所示:
<button...><span>My Button</span></button>
使用Zend_Form执行此操作的最佳方法是什么?
答案 0 :(得分:7)
我已经尝试过,并且最终使用相同的方法无法实现这一目标。似乎最简单的方法是:
...
$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中。也就是说,这是一个开始,它解决了你的问题。
答案 1 :(得分:5)
你可以这样做:
$this->addElement(new Zend_Form_Element_Button(
'send',
array(
'label' => '<span>registrieren</span>',
'class' => 'button-red',
'type' => 'submit',
'escape' => false,
'required' => false,
'ignore' => false,
)
));
答案 2 :(得分:1)
只是逃避标签工作正常。只有当某人开始搞乱标签装饰器时才会中断(通常在自定义标签时发生)
您可以使用Zend_Form函数 setElementDecorators()并从样式中排除一些元素。请阅读Zend_Form Manual中的示例#3为某些元素设置装饰器(包括注意!)以获取更多详细信息。
对于那些仍然感到困惑的人,这里有一个示例代码,用于将表单设置为表格,并且有一个以双跨度封装的按钮:
//(...)putting some other elements in the form first
//now add submit button
$form ->addElement('button', 'submit', array(
'label' => '<span><span>SUBMIT</span></span>',
'value' => 'submit'
));
$form
//set decorators for non-button elements
->setElementDecorators(array(
'ViewHelper',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
array('Label', array('tag' => 'td')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))), array('submit'), false)
//and then for button elements only
->setElementDecorators(array(
'ViewHelper',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))), array('submit'), true)
//and finally for the form
->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'table')),
'Form'
));
其中 $ form 是Zend_Form元素。希望这有帮助!
答案 3 :(得分:0)
我会在按钮中添加一个类,然后在CSS中定义该类,使其像跨度一样。我相信你不仅仅想要做到这一点,而不仅仅是坚持下去。
再添加一个setAttrib()调用:
$submit->setAttrib('class', 'submitButton');
然后在你的CSS中:
.submitButton {
display:inline;
font-weight:bold; /* Or whatever you're wanting to do */
}
我添加了display:inline,因为这会使按钮像跨度一样。这应该让你至少开始。当然,您也可以将CSS基于按钮的id,只需使用display:inline来获得跨度行为。
答案 4 :(得分:0)
尝试
$element->addDecorator('Label', array('tag' => 'span', 'class' => 'aClass'))