Skip to content

Commit d9d4802

Browse files
daFishxabbuh
daFish
authored andcommitted
Created paragraph for prototype customization.
1 parent 1750b9b commit d9d4802

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

cookbook/form/form_collections.rst

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,5 +729,79 @@ the relationship between the removed ``Tag`` and ``Task`` object.
729729
updated (whether you're adding new tags or removing existing tags) on
730730
each Tag object itself.
731731

732+
.. _cookbook-form-collections-custom-prototype
733+
734+
Render a custom prototype
735+
-------------------------
736+
737+
Most of the time the provided prototype will be sufficient for your needs
738+
and does not need to be changed. But if you are in the situation were
739+
you need to have a complete custom prototype you can render it yourself:
740+
741+
.. configuration-block::
742+
743+
.. code-block:: html+jinja
744+
745+
<!-- src/Acme/TaskBundle/Resources/views/Task/prototypeTask.html.twig -->
746+
data-prototype="{% filter escape %}
747+
{% include 'AcmeTaskBundle:Task:prototypeTask.html.twig'
748+
with { 'task': form.task.get('prototype') }
749+
%}
750+
{% endfilter %}"
751+
752+
.. code-block:: html+php
753+
754+
<!-- src/Acme/TaskBundle/Resources/views/Task/prototypeTask.html.php -->
755+
data-prototype="<?php
756+
$prototype = $view->render(
757+
'AcmeTaskBundle:Task:prototypeTask.html.php',
758+
array('task' => $form->task->get('prototype'))
759+
);
760+
761+
echo $view->escape($prototype);
762+
?>"
763+
764+
To be not confused let's have a look how the prototype-template might look like.
765+
766+
.. configuration-block::
767+
768+
.. code-block:: html+jinja
769+
770+
<tr>
771+
<td>{{ form_widget(task.task) }}</td>
772+
<td>{{ form_widget(task.dueDate) }}</td>
773+
</tr>
774+
775+
.. code-block:: html+php
776+
777+
<tr>
778+
<td><?php echo $view['form']->widget($task->getTask()) ?></td>
779+
<td><?php echo $view['form']->widget($task->getDueDate()) ?></td>
780+
</tr>
781+
782+
The included template contains the markup used for the prototype.
783+
This way you can not only easily structure your prototype-markup,
784+
you can also use this markup to render the
785+
contents of the collection when it already holds items:
786+
787+
.. configuration-block::
788+
789+
.. code-block:: html+jinja
790+
791+
{% for task in tasks %}
792+
{% include 'AcmeTaskBundle:Task:prototypeTask.html.twig'
793+
with { 'form': form.task.vars.form }
794+
%}
795+
{% endfor %}
796+
797+
.. code-block:: html+php
798+
799+
<?php foreach ($tasks as $task) ?>
800+
<?php echo $view->render('AcmeTaskBundle:Task:prototypeTask.html.php', array('form' => $form->task->vars->form)); ?>
801+
<?php endforeach; ?>
802+
803+
This makes sure the displayed items are the same as the newly inserted
804+
from the prototype.
805+
732806
.. _`Owning Side and Inverse Side`: http://docs.doctrine-project.org/en/latest/reference/unitofwork-associations.html
733807
.. _`JSFiddle`: http://jsfiddle.net/847Kf/4/

0 commit comments

Comments
 (0)