本文介绍了Zend 框架 Zend_Form 装饰器:<span>内部按钮元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个按钮元素,如下所示:

I have a button element that I've created like so:

$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:

This generates the following HTML:

<li>
    <label for="submit" class="optional">My Button</label> 
    <button name="submit" id="submit" type="submit">My Button</button>
</li>

我想用 <span> 包裹按钮的内部,如下所示:

I would like to wrap the inside of the button with a <span>, like this:

<button...><span>My Button</span></button>

使用 Zend_Form 执行此操作的最佳方法是什么?

What is the best way to do this using Zend_Form?

推荐答案

我自己尝试过,但最终未能使用相同的方法实现这一目标.似乎最简单的方法是这样做:

I have tried and, ultimately, failed to achieve this myself using the same approach. It would seem that the easiest way to do this would be to do:

...
$submit->setLabel('<span>My Button</span>');
...

但是,跨度将被转义.完全有可能关闭标签装饰器的转义,但是,添加标签装饰器会错误地呈现输出,例如:

However, the span will be escaped. It's perfectly possible to turn off the escaping of a lable decorator, however, adding a label decorator renders the output incorrectly, for instance:

$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">&lt;span&gt;My Button&lt;/span&gt</button>
</li>

...除了在语义上不正确(易于修复)之外,它仍在转义元素内的 span 标签.

...which, aside from being semantically incorrect (easily fixable), is still escaping the span tags inside the element.

那你在做什么?

嗯,我认为最好的方法(这是我在严格控制 Zend_Form 渲染时的元建议)是使用 ViewScript 装饰器.

Well I think the best approach (and this is my meta-advice when it comes to tight control over Zend_Form rendering) is to use the ViewScript decorator.

$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 中定义以下内容:

...then in _submitButton.phtml define the following:

<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')).

The _submitButton.phtml file will need to be in a view script directory (you might be best adding a specific one for your form decorators using $view->addScriptPath('/path/to/my/form/decorators')).

这应该呈现您正在寻找的内容.由于我在工作中遇到的灵活性问题,我才刚刚开始研究 ViewScript 装饰器.您会注意到我的脚本不是那么灵活,当然也不在 BNF 中,因为所有成员都可以填充到元素对象上.也就是说,这是一个开始,它可以解决您的问题.

This should render what you're looking for. I've only just started looking at the ViewScript decorator due to flexibility issues I'm experiencing in work. You'll notice that my script isn't that flexible, and certainly isn't in BNF, given all the members that can be populated on the element object. That said, it's a start and it solves your problem.

这篇关于Zend 框架 Zend_Form 装饰器:&lt;span&gt;内部按钮元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 10:40