如何在Zend2中为集合呈现自定义模板

时间:2015-02-11 18:04:04

标签: php zend-framework zend-framework2 zend-form

形式:

$this->add(array(
        'type' => 'Zend\Form\Element\Collection',
        'name' => 'openHours',
        'options' => array(
            'label' => 'Hours',
            'count' => 2,
            'should_create_template' => true,
            'template_placeholder' => '__placeholder__',
            'target_element' => array(
                'type' => 'Listings\Form\OpenHourFieldset',
            ),
        ),
    ));

in form.phtml

<?php $openHoursCollection = $form->get('openHours'); ?>
    <?php foreach ($openHoursCollection as $collection){ ?>
        <fieldset>
            <?php echo $this->formSelect($collection->get('weekday')); ?>
        </fieldset>
    <?php } ?>
    <?php echo $this->formCollection->renderTemplate($openHoursCollection); ?>

当在zend2中迭代集合项以使其正常工作时,它还需要一个自定义模板。但如何获得$this->formCollection->renderTemplate($collection) 呈现自定义HTML? 我需要将所有这些集合渲染定制为

任何人都知道吗? 感谢

1 个答案:

答案 0 :(得分:3)

我不知道这是否是最佳方法,但我通过创建自己的视图助手来渲染和模板来在浏览器中的JavaSrcript中动态添加项目来解决这个问题。

创建视图助手,扩展标准表单集合视图助手:

<?php

namespace App\View\Helper;

use Zend\Form\View\Helper\FormCollection;
use Zend\Form\ElementInterface;
use Zend\Form\Element\Collection as CollectionElement;
use Zend\Form\FieldsetInterface;

class ItemListFormCollection extends FormCollection
{
    private $elementformat =
            '<div class="form-group input-group input-group-sm">%s
                <span class="input-group-btn">
                    <button class="btn btn-default delete" type="button">Delete</button>
                </span>
            </div>';

    public function __invoke(ElementInterface $element = null, $wrap = true)
    {
        if(!$element){
            return $this;
        }
        $this->setShouldWrap($wrap);
        return $this->render($element);
    }

    public function render(ElementInterface $element)
    {
        $renderer = $this->getView();
        if (!method_exists($renderer, 'plugin')) {
            return '';
        }

        $markup           = '';
        $templateMarkup   = '';
        $escapeHtmlHelper = $this->getEscapeHtmlHelper();
        $elementHelper    = $this->getElementHelper();
        $fieldsetHelper   = $this->getFieldsetHelper();

        if ($element instanceof CollectionElement && $element->shouldCreateTemplate()) {
            $templateMarkup = $this->renderTemplate($element);
        }

        foreach ($element->getIterator() as $elementOrFieldset) {
            if ($elementOrFieldset instanceof FieldsetInterface) {
                $markup .= sprintf($this->elementformat,$fieldsetHelper($elementOrFieldset));
            } elseif ($elementOrFieldset instanceof ElementInterface) {
                $markup .= $elementHelper($elementOrFieldset);
            }
        }

        if (!empty($templateMarkup)) {
            $markup .= $templateMarkup;
        }

        return $markup;
    }

    public function renderTemplate(CollectionElement $collection)
    {
        $elementHelper          = $this->getElementHelper();
        $escapeHtmlAttribHelper = $this->getEscapeHtmlAttrHelper();
        $templateMarkup         = '';

        $elementOrFieldset = $collection->getTemplateElement();

        if ($elementOrFieldset instanceof FieldsetInterface) {
            $templateMarkup .= $this->render($elementOrFieldset);
        } elseif ($elementOrFieldset instanceof ElementInterface) {
            $templateMarkup .= $elementHelper($elementOrFieldset);
        }

        $element = sprintf($this->elementformat, $templateMarkup);

        return sprintf(
            '<span data-template="%s"></span>',
            $escapeHtmlAttribHelper($element)
        );
    }
}

您可以将$elementformat变量修改为您自己的格式。

下一步你应该在config / module.config.php的视图助手服务管理器中注册这个帮助器:

...
'view_helpers' => array(
    'invokables' => array(
        'itemListFormCollection' => 'App\View\Helper\ItemListFormCollection'
    )
),
...

现在你可以在任何视图文件中将它用作原始的formCollection助手:

<?php echo $this->itemListFormCollection($form->get('collection')); ?>