ZF_FORM:如何为表单元素添加一些视图模板?

时间:2011-06-03 13:45:25

标签: php model-view-controller zend-framework zend-form zend-decorators

我在表单对象中创建了元素:

function createElement()
{
    $template = new Zend_Form_Element_Hidden('field');
    $template->addDecorator('ViewScript', array('placement' => 'prepend', 'viewModule' => 'admin', 'viewScript' => 'values.phtml'))

   $this->addElement($template);
}

function setViewTemplate($values)
{
     $view = new Zend_View();
     $view->setScriptPath(APPLICATION_PATH . '/scripts/');
     $view->assign('values', $values);

     $this->getElement('field')->setView($view);
}

但在视图脚本'values.phtml'中,我无法访问$ this->值等值。 我在这里做错了什么? 我知道添加自己的装饰器会很好,但使用zends的装饰器很有意思。

2 个答案:

答案 0 :(得分:1)

您可以使用attribs解析它

$template->setAttrib('key', 'value');

并在模板中

<?php echo $this->element->getAttrib('key'); ?>

答案 1 :(得分:1)

来自Zend Framework文档:Standard Form Decorators Shipped With Zend Framework部分 的 Zend_Form_Decorator_ViewScript

  

此外,所有选项都传递给   装饰器通过setOptions()来实现的   不在内部使用(例如   放置,分隔符等)通过   视图脚本作为视图变量。

function setViewTemplate($values)
{
     $this->getElement('field')
          ->getDecorator('ViewScript')
          ->setOptions('values', $values);
}