如何从其内容中单独打印组显示? [英] How to print a group display individually from its content?

查看:144
本文介绍了如何从其内容中单独打印组显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Zend Framework和Zend_Form来渲染我的表单。但是,由于我发现很难自定义它,我决定单独打印元素。

I'm using Zend Framework and Zend_Form to render my form. But as I found it hard to customize it, I decided to print elements individually.

问题是,我不知道如何在显示组中打印单个元素。我知道如何打印我的显示组(fieldsets),但我需要在里面添加一些东西(如< div class =spacer>< / div> 取消 float:left

Problem is, I don't know how to print individual elements inside a display group. I know how to print my display groups (fieldsets) but I need to add something inside it (like a <div class="spacer"></div> to cancel the float:left.

有没有任何方法只显示组,

Is there any way to display the group only without its content so I can print them individually myself?

感谢您的帮助。

推荐答案

你正在寻找的是'ViewScript'装饰器,它允许你以任何你需要的方式形成你的html。这是一个简单的例子如何工作:

What you are looking for is the 'ViewScript' decorator. It allows you to form your html in any way you need. Here is a simple example of how it works:

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

The form, a simple search form:

<?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);
    }
}



接下来是'partial'这是装饰器,这里是你建立html你想要的位置:

Next is the 'partial' this is the decorator, here is where you build the html how you want it:

<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?> 。

display this form in your view script with the normal <?php $this->form ?>.

用Zend_Form构建。因此,添加任何元素到您自己的字段集将很简单。

You can use this method for any form you want to build with Zend_Form. So adding any element to your own fieldset would be simple.

这篇关于如何从其内容中单独打印组显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆