Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Allow to render template separately #2417

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions library/Zend/Form/View/Helper/FormCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,7 @@ public function render(ElementInterface $element)
$elementHelper = $this->getElementHelper();

if ($element instanceof CollectionElement && $element->shouldCreateTemplate()) {
$elementOrFieldset = $element->getTemplateElement();

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

foreach ($element->getIterator() as $elementOrFieldset) {
Expand All @@ -83,12 +77,7 @@ public function render(ElementInterface $element)

// If $templateMarkup is not empty, use it for simplify adding new element in JavaScript
if (!empty($templateMarkup)) {
$escapeHtmlAttribHelper = $this->getEscapeHtmlAttrHelper();

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

// Every collection is wrapped by a fieldset if needed
Expand All @@ -109,6 +98,32 @@ public function render(ElementInterface $element)
return $markup;
}

/**
* Only render a template
*
* @param CollectionElement $collection
* @return string
*/
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);
}

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

/**
* Invoke helper as function
*
Expand Down
9 changes: 9 additions & 0 deletions tests/ZendTest/Form/View/Helper/FormCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,14 @@ public function testSetDefaultElementHelperToFoo()
$this->assertSame('foo', $defaultElement);
}

public function testCanRenderTemplateAlone()
{
$form = $this->getForm();
$collection = $form->get('colors');
$collection->setShouldCreateTemplate(true);

$markup = $this->helper->renderTemplate($collection);
$this->assertContains('<span data-template', $markup);
$this->assertContains($collection->getTemplatePlaceholder(), $markup);
}
}