如何从它的内​​容单独打印一组显示?(How to print a group display in

2019-06-24 00:06发布

我使用Zend框架和Zend_Form的渲染我的形式。 但是,当我发现很难定制它,我决定单独打印元素。

问题是,我不知道如何打印显示组内的单个元素。 我知道如何打印我的显示组(字段集),但我需要补充的东西里面(如<div class="spacer"></div>取消float:left

有什么办法不只是其内容显示该组,所以我可以单独打印他们自己?

谢谢您的帮助。

Answer 1:

你要找的是“ViewScript”装饰。 它可以让你形成任何你需要的方式您的HTML。 这里是它如何工作的一个简单的例子:

形式,一个简单的搜索页面:

<?php
class Application_Form_Search extends Zend_Form
{
    public function init() {
        // create new element
        $query = $this->createElement('text', 'query');
        // element options
        $query->setLabel('Search Keywords');
        $query->setAttribs(array('placeholder' => 'Query String',
            'size' => 27,
            ));
        // add the element to the form
        $this->addElement($query);
        //build submit button
        $submit = $this->createElement('submit', 'search');
        $submit->setLabel('Search Site');
        $this->addElement($submit);
    }
}

接下来是“部分”,这是装饰,在这里你建立HTML你想要的:

<article class="search">
<!-- I get the action and method from the form but they were added in the controller -->
    <form action="<?php echo $this->element->getAction() ?>"
          method="<?php echo $this->element->getMethod() ?>">
        <table>
            <tr>
            <!-- renderLabel() renders the Label decorator for the element
                <th><?php echo $this->element->query->renderLabel() ?></th>
            </tr>
            <tr>
            <!-- renderViewHelper() renders the actual input element, all decorators can be accessed this way -->
                <td><?php echo $this->element->query->renderViewHelper() ?></td>
            </tr>
            <tr>
            <!-- this line renders the submit element as a whole -->
                <td><?php echo $this->element->search ?></td>
            </tr>
        </table> 
    </form>
</article>

最后的控制器编码:

public function preDispatch() {
        //I put this in the preDispatch method because I use it for every action and have it assigned to a placeholder.
        //initiate form
        $searchForm = new Application_Form_Search();
        //set form action
        $searchForm->setAction('/index/display');
        //set label for submit button
        $searchForm->search->setLabel('Search Collection');
        //I add the decorator partial here. The partial .phtml lives under /views/scripts
        $searchForm->setDecorators(array(
            array('ViewScript', array(
                    'viewScript' => '_searchForm.phtml'
            ))
        ));
        //assign the search form to the layout place holder
        //substitute $this->view->form = $form; for a normal action/view
        $this->_helper->layout()->search = $searchForm;
    }

这显示在表格视图脚本与正常<?php $this->form ?>

您可以使用您想要建立和Zend_Form任何形式的这种方法。 所以添加任何元素,以自己的字段集将是简单。



文章来源: How to print a group display individually from its content?