Permalink
Browse files

Allow to render template separately

  • Loading branch information...
bakura10 committed Sep 25, 2012
1 parent c620291 commit 58b3424c9c13fa68405effcf59ed5779749984bd
@@ -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
@@ -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
*
@@ -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);
+ }
}

0 comments on commit 58b3424

Please sign in to comment.