Skip to content

Removed the code of the PHP templates #10014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2018
Merged
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
37 changes: 9 additions & 28 deletions controller/upload_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,16 @@ Now, update the template that renders the form to display the new ``brochure``
field (the exact template code to add depends on the method used by your application
to :doc:`customize form rendering </form/form_customization>`):

.. configuration-block::

.. code-block:: html+twig

{# app/Resources/views/product/new.html.twig #}
<h1>Adding a new product</h1>

{{ form_start(form) }}
{# ... #}

{{ form_row(form.brochure) }}
{{ form_end(form) }}
.. code-block:: html+twig

.. code-block:: html+php
{# app/Resources/views/product/new.html.twig #}
<h1>Adding a new product</h1>

<!-- app/Resources/views/product/new.html.php -->
<h1>Adding a new product</h1>
{{ form_start(form) }}
{# ... #}

<?php echo $view['form']->start($form) ?>
<?php echo $view['form']->row($form['brochure']) ?>
<?php echo $view['form']->end($form) ?>
{{ form_row(form.brochure) }}
{{ form_end(form) }}

Finally, you need to update the code of the controller that handles the form::

Expand Down Expand Up @@ -197,17 +186,9 @@ There are some important things to consider in the code of the above controller:

You can use the following code to link to the PDF brochure of a product:

.. configuration-block::

.. code-block:: html+twig

<a href="{{ asset('uploads/brochures/' ~ product.brochure) }}">View brochure (PDF)</a>

.. code-block:: html+php
.. code-block:: html+twig

<a href="<?php echo $view['assets']->getUrl('uploads/brochures/'.$product->getBrochure()) ?>">
View brochure (PDF)
</a>
<a href="{{ asset('uploads/brochures/' ~ product.brochure) }}">View brochure (PDF)</a>

.. tip::

Expand Down
34 changes: 9 additions & 25 deletions doctrine/registration_form.rst
Original file line number Diff line number Diff line change
Expand Up @@ -363,34 +363,18 @@ the :ref:`user password encoding <security-encoding-user-password>` article.

Next, create the template:

.. configuration-block::

.. code-block:: html+twig

{# app/Resources/views/registration/register.html.twig #}

{{ form_start(form) }}
{{ form_row(form.username) }}
{{ form_row(form.email) }}
{{ form_row(form.plainPassword.first) }}
{{ form_row(form.plainPassword.second) }}

<button type="submit">Register!</button>
{{ form_end(form) }}

.. code-block:: html+php

<!-- app/Resources/views/registration/register.html.php -->
.. code-block:: html+twig

<?php echo $view['form']->start($form) ?>
<?php echo $view['form']->row($form['username']) ?>
<?php echo $view['form']->row($form['email']) ?>
{# app/Resources/views/registration/register.html.twig #}

<?php echo $view['form']->row($form['plainPassword']['first']) ?>
<?php echo $view['form']->row($form['plainPassword']['second']) ?>
{{ form_start(form) }}
{{ form_row(form.username) }}
{{ form_row(form.email) }}
{{ form_row(form.plainPassword.first) }}
{{ form_row(form.plainPassword.second) }}

<button type="submit">Register!</button>
<?php echo $view['form']->end($form) ?>
<button type="submit">Register!</button>
{{ form_end(form) }}

See :doc:`/form/form_customization` for more details.

Expand Down
18 changes: 3 additions & 15 deletions form/action_method.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,10 @@ options:
Finally, you can override the action and method in the template by passing them
to the ``form()`` or the ``form_start()`` helper functions:

.. configuration-block::

.. code-block:: html+twig

{# app/Resources/views/default/new.html.twig #}
{{ form_start(form, {'action': path('target_route'), 'method': 'GET'}) }}
.. code-block:: html+twig

.. code-block:: html+php

<!-- app/Resources/views/default/new.html.php -->
<?php echo $view['form']->start($form, array(
// The path() method was introduced in Symfony 2.8. Prior to 2.8,
// you had to use generate().
'action' => $view['router']->path('target_route'),
'method' => 'GET',
)) ?>
{# app/Resources/views/default/new.html.twig #}
{{ form_start(form, {'action': path('target_route'), 'method': 'GET'}) }}

.. note::

Expand Down
59 changes: 20 additions & 39 deletions form/create_custom_field_type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,45 +123,26 @@ But for the sake of this example, suppose that when your field is "expanded"
always render it in a ``ul`` element. In your form theme template (see above
link for details), create a ``shipping_widget`` block to handle this:

.. configuration-block::

.. code-block:: html+twig

{# app/Resources/views/form/fields.html.twig #}
{% block shipping_widget %}
{% spaceless %}
{% if expanded %}
<ul {{ block('widget_container_attributes') }}>
{% for child in form %}
<li>
{{ form_widget(child) }}
{{ form_label(child) }}
</li>
{% endfor %}
</ul>
{% else %}
{# just let the choice widget render the select tag #}
{{ block('choice_widget') }}
{% endif %}
{% endspaceless %}
{% endblock %}

.. code-block:: html+php

<!-- app/Resources/views/form/shipping_widget.html.php -->
<?php if ($expanded) : ?>
<ul <?php $view['form']->block($form, 'widget_container_attributes') ?>>
<?php foreach ($form as $child) : ?>
<li>
<?php echo $view['form']->widget($child) ?>
<?php echo $view['form']->label($child) ?>
</li>
<?php endforeach ?>
</ul>
<?php else : ?>
<!-- just let the choice widget render the select tag -->
<?php echo $view['form']->renderBlock('choice_widget') ?>
<?php endif ?>
.. code-block:: html+twig

{# app/Resources/views/form/fields.html.twig #}
{% block shipping_widget %}
{% spaceless %}
{% if expanded %}
<ul {{ block('widget_container_attributes') }}>
{% for child in form %}
<li>
{{ form_widget(child) }}
{{ form_label(child) }}
</li>
{% endfor %}
</ul>
{% else %}
{# just let the choice widget render the select tag #}
{{ block('choice_widget') }}
{% endif %}
{% endspaceless %}
{% endblock %}

.. tip::

Expand Down
32 changes: 11 additions & 21 deletions form/create_form_type_extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -266,31 +266,21 @@ In your extension class, you have added a new variable (``image_url``), but
you still need to take advantage of this new variable in your templates.
Specifically, you need to override the ``file_widget`` block:

.. configuration-block::

.. code-block:: html+twig

{# src/AppBundle/Resources/views/Form/fields.html.twig #}
{% extends 'form_div_layout.html.twig' %}

{% block file_widget %}
{% spaceless %}
.. code-block:: html+twig

{{ block('form_widget') }}
{% if image_url is not null %}
<img src="{{ asset(image_url) }}"/>
{% endif %}
{# src/AppBundle/Resources/views/Form/fields.html.twig #}
{% extends 'form_div_layout.html.twig' %}

{% endspaceless %}
{% endblock %}
{% block file_widget %}
{% spaceless %}

.. code-block:: html+php
{{ block('form_widget') }}
{% if image_url is not null %}
<img src="{{ asset(image_url) }}"/>
{% endif %}

<!-- src/AppBundle/Resources/views/Form/file_widget.html.php -->
<?php echo $view['form']->widget($form) ?>
<?php if (null !== $image_url): ?>
<img src="<?php echo $view['assets']->getUrl($image_url) ?>"/>
<?php endif ?>
{% endspaceless %}
{% endblock %}

.. note::

Expand Down
105 changes: 34 additions & 71 deletions form/dynamic_form_modification.rst
Original file line number Diff line number Diff line change
Expand Up @@ -585,77 +585,40 @@ your application. Assume that you have a sport meetup creation controller::
The associated template uses some JavaScript to update the ``position`` form
field according to the current selection in the ``sport`` field:

.. configuration-block::

.. code-block:: html+twig

{# app/Resources/views/meetup/create.html.twig #}
{{ form_start(form) }}
{{ form_row(form.sport) }} {# <select id="meetup_sport" ... #}
{{ form_row(form.position) }} {# <select id="meetup_position" ... #}
{# ... #}
{{ form_end(form) }}

<script>
var $sport = $('#meetup_sport');
// When sport gets selected ...
$sport.change(function() {
// ... retrieve the corresponding form.
var $form = $(this).closest('form');
// Simulate form data, but only include the selected sport value.
var data = {};
data[$sport.attr('name')] = $sport.val();
// Submit data via AJAX to the form's action path.
$.ajax({
url : $form.attr('action'),
type: $form.attr('method'),
data : data,
success: function(html) {
// Replace current position field ...
$('#meetup_position').replaceWith(
// ... with the returned one from the AJAX response.
$(html).find('#meetup_position')
);
// Position field now displays the appropriate positions.
}
});
});
</script>

.. code-block:: html+php

<!-- app/Resources/views/Meetup/create.html.php -->
<?php echo $view['form']->start($form) ?>
<?php echo $view['form']->row($form['sport']) ?> <!-- <select id="meetup_sport" ... -->
<?php echo $view['form']->row($form['position']) ?> <!-- <select id="meetup_position" ... -->
<!-- ... -->
<?php echo $view['form']->end($form) ?>

<script>
var $sport = $('#meetup_sport');
// When sport gets selected ...
$sport.change(function() {
// ... retrieve the corresponding form.
var $form = $(this).closest('form');
// Simulate form data, but only include the selected sport value.
var data = {};
data[$sport.attr('name')] = $sport.val();
// Submit data via AJAX to the form's action path.
$.ajax({
url : $form.attr('action'),
type: $form.attr('method'),
data : data,
success: function(html) {
// Replace current position field ...
$('#meetup_position').replaceWith(
// ... with the returned one from the AJAX response.
$(html).find('#meetup_position')
);
// Position field now displays the appropriate positions.
}
});
});
</script>
.. code-block:: html+twig

{# app/Resources/views/meetup/create.html.twig #}
{{ form_start(form) }}
{{ form_row(form.sport) }} {# <select id="meetup_sport" ... #}
{{ form_row(form.position) }} {# <select id="meetup_position" ... #}
{# ... #}
{{ form_end(form) }}

<script>
var $sport = $('#meetup_sport');
// When sport gets selected ...
$sport.change(function() {
// ... retrieve the corresponding form.
var $form = $(this).closest('form');
// Simulate form data, but only include the selected sport value.
var data = {};
data[$sport.attr('name')] = $sport.val();
// Submit data via AJAX to the form's action path.
$.ajax({
url : $form.attr('action'),
type: $form.attr('method'),
data : data,
success: function(html) {
// Replace current position field ...
$('#meetup_position').replaceWith(
// ... with the returned one from the AJAX response.
$(html).find('#meetup_position')
);
// Position field now displays the appropriate positions.
}
});
});
</script>

The major benefit of submitting the whole form to just extract the updated
``position`` field is that no additional server-side code is needed; all the
Expand Down
27 changes: 7 additions & 20 deletions form/embedded.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,29 +109,16 @@ the ``TaskType`` class.

Render the ``Category`` fields in the same way as the original ``Task`` fields:

.. configuration-block::
.. code-block:: html+twig

.. code-block:: html+twig
{# ... #}

{# ... #}
<h3>Category</h3>
<div class="category">
{{ form_row(form.category.name) }}
</div>

<h3>Category</h3>
<div class="category">
{{ form_row(form.category.name) }}
</div>

{# ... #}

.. code-block:: html+php

<!-- ... -->

<h3>Category</h3>
<div class="category">
<?php echo $view['form']->row($form['category']['name']) ?>
</div>

<!-- ... -->
{# ... #}

When the user submits the form, the submitted data for the ``Category`` fields
are used to construct an instance of ``Category``, which is then set on the
Expand Down
Loading