如何以 Zend 形式呈现一些 html? [英] How to render some html in Zend form?
问题描述
在我的表单中,我需要这个:
In my form i need this:
<li class ="af_title"><div>Contact form</div></li>
我明白,如果我需要一个文本字段——那么我只需创建 Zend_Form_Element_Text 并在它周围包裹 HtmlTag 装饰器——对吗?
I understand that if I need for example a text field - then I'd just create Zend_Form_Element_Text and wrap HtmlTag decorator around it - right?
问题:
1) 我将如何生成 div 表单元素,以便我可以将装饰器包裹在它周围?
1) how would I generate div form element so I can wrap decorator around it?
2) 嗯.- 如何将所述元素的内容设置为联系表单"?
2) mmm. - how would I set content of said element to be "Contact form"?
谢谢:)
推荐答案
将您的联系表单包裹在 内的
div
周围> 你可以这样做:
To wrap your Contact form around div
which is inside <li class ="af_title">
you can do as follows:
$yourForm = new YourForm();
$yourForm->addDecorator(array('div' => 'HtmlTag'), array('tag' => 'div'));
$yourForm->addDecorator(array('li' => 'HtmlTag'), array('tag' => 'li', 'class' => 'af_title'));
我看到您说的是 div 表单元素.此类元素不存在,但您可以轻松创建它.为此,您需要两件事:新的表单元素(即 div 元素)和一个负责生成 html 的表单视图助手.我允许自己准备这两个元素的简单示例来说明我的意思:
I see that you speak of div form element. Such element does not exist, but you could easily create it. For this you need two things: new form element (i.e. div element) and a form view helper that would take care of generation of the html. I allowed myself to prepare simple examples of these two elements to show what I mean:
APPLICATION_PATH 中的 Div.php./forms/Element:
class My_Form_Element_Div extends Zend_Form_Element {
/**
* Default form view helper to use for rendering
* @var string
*/
public $helper = 'formDiv';
public function __construct($spec, $options = null) {
parent::__construct($spec, $options);
$this->removeDecorator('label');
$this->removeDecorator('htmlTag');
}
}
APPLICATION_PATH 中的 FormDiv.php ./views/helpers:
class My_View_Helper_FormDiv extends Zend_View_Helper_FormElement {
public function formDiv($name, $value = null, $attribs = null) {
$class = '';
if (isset($attribs['class'])) {
$class = 'class = "'. $attribs['class'] .'"';
}
return "<li $class><div>$value</div></li>";
}
}
有了这个,您可以将 div 表单元素添加到 init() 方法中的任何表单元素,如下所示:
Having this, you can just add the div form element to any form element in their init() method as follows:
$div = new My_Form_Element_Div('mydiv');
$div->setValue('Contact form')->setAttrib('class', 'af_title');
$this->addElement($div);
这篇关于如何以 Zend 形式呈现一些 html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!