Skip to content

Commit 98b611d

Browse files
author
daFish
committed
Added PHP examples.
Added example for prototype template.
1 parent 18927dc commit 98b611d

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

cookbook/form/form_collections.rst

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,15 +667,42 @@ you need to have a complete custom prototype you can render it yourself:
667667

668668
.. code-block:: html+jinja
669669

670+
<!-- src/Acme/TaskBundle/Resources/views/Task/prototypeTask.html.twig -->
670671
data-prototype="{% filter escape %}
671672
{% include 'AcmeTaskBundle:Task:prototypeTask.html.twig'
672-
with { 'form': form.task.get('prototype') }
673+
with { 'task': form.task.get('prototype') }
673674
%}
674675
{% endfilter %}"
675676

676-
The included ``AcmeTaskBundle:Task:prototypeTask.html.twig`` contains the
677-
markup used for the prototype. This way you can not only easily structure
678-
your prototype-markup, you can also use this markup to render the
677+
.. code-block:: html+php
678+
679+
<!-- src/Acme/TaskBundle/Resources/views/Task/prototypeTask.html.php -->
680+
data-prototype="<?php
681+
$prototype = $view->render(
682+
'AcmeTaskBundle:Task:prototypeTask.html.php',
683+
array('task' => $form->task->get('prototype')
684+
);
685+
686+
echo $view->escape($prototype);
687+
?>"
688+
689+
To be not confused let's have a look how the prototype-template might look like.
690+
691+
.. code-block:: html+jinja
692+
<tr>
693+
<td>{{ form_widget(task.task) }}</td>
694+
<td>{{ form_widget(task.dueDate) }}</td>
695+
</tr>
696+
697+
.. code-block:: html+php
698+
<tr>
699+
<td><?php echo $view['form']->widget($task->getTask()) ?></td>
700+
<td><?php echo $view['form']->widget($task->getDueDate()) ?></td>
701+
</tr>
702+
703+
The included template contains the markup used for the prototype.
704+
This way you can not only easily structure your prototype-markup,
705+
you can also use this markup to render the
679706
contents of the collection when it already holds items:
680707

681708
.. code-block:: html+jinja
@@ -686,6 +713,12 @@ contents of the collection when it already holds items:
686713
%}
687714
{% endfor %}
688715

716+
.. code-block:: html+php
717+
718+
<?php foreach ($tasks as $task) ?>
719+
<?php echo $view->render('AcmeTaskBundle:Task:prototypeTask.html.php', array('form' => $form->task->vars->form)); ?>
720+
<?php endforeach; ?>
721+
689722
This makes sure the displayed items are the same as the newly inserted
690723
from the prototype.
691724

0 commit comments

Comments
 (0)