Skip to content

Loading…

Allow to render template separately #2417

Closed
wants to merge 1 commit into from

2 participants

@bakura10

When dealing with complex form structures, calling view helpers directly does not offer enough flexibility. However, it is still useful to be able to have some template in Collections.

Therefore, this PR simply allows the Collection view helper to render only a template for JavaScript purposes.

@ghost Unknown pushed a commit that referenced this pull request
@weierophinney weierophinney Merge branch 'feature/form-collection-render' into develop
Close #2417
c5ed2fc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 25, 2012
  1. @bakura10
View
41 library/Zend/Form/View/Helper/FormCollection.php
@@ -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) {
@@ -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
@@ -110,6 +99,32 @@ public function render(ElementInterface $element)
}
/**
+ * 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
*
* Proxies to {@link render()}.
View
9 tests/ZendTest/Form/View/Helper/FormCollectionTest.php
@@ -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);
+ }
}
Something went wrong with that request. Please try again.