diff --git a/controller/upload_file.rst b/controller/upload_file.rst index f65594177b6..500f1fda9b7 100644 --- a/controller/upload_file.rst +++ b/controller/upload_file.rst @@ -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 `): -.. configuration-block:: - - .. code-block:: html+twig - - {# app/Resources/views/product/new.html.twig #} -

Adding a new product

- - {{ 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 #} +

Adding a new product

- -

Adding a new product

+ {{ form_start(form) }} + {# ... #} - start($form) ?> - row($form['brochure']) ?> - end($form) ?> + {{ form_row(form.brochure) }} + {{ form_end(form) }} Finally, you need to update the code of the controller that handles the form:: @@ -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 - - View brochure (PDF) - - .. code-block:: html+php +.. code-block:: html+twig - - View brochure (PDF) - + View brochure (PDF) .. tip:: diff --git a/doctrine/registration_form.rst b/doctrine/registration_form.rst index 3db629e3349..f5b04d13769 100644 --- a/doctrine/registration_form.rst +++ b/doctrine/registration_form.rst @@ -363,34 +363,18 @@ the :ref:`user password encoding ` 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) }} - - - {{ form_end(form) }} - - .. code-block:: html+php - - +.. code-block:: html+twig - start($form) ?> - row($form['username']) ?> - row($form['email']) ?> + {# app/Resources/views/registration/register.html.twig #} - row($form['plainPassword']['first']) ?> - 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) }} - - end($form) ?> + + {{ form_end(form) }} See :doc:`/form/form_customization` for more details. diff --git a/form/action_method.rst b/form/action_method.rst index 5178a917887..fa564bdf35d 100644 --- a/form/action_method.rst +++ b/form/action_method.rst @@ -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 - - - 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:: diff --git a/form/create_custom_field_type.rst b/form/create_custom_field_type.rst index d8ef5e3cb49..483b69901f2 100644 --- a/form/create_custom_field_type.rst +++ b/form/create_custom_field_type.rst @@ -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 %} - - {% else %} - {# just let the choice widget render the select tag #} - {{ block('choice_widget') }} - {% endif %} - {% endspaceless %} - {% endblock %} - - .. code-block:: html+php - - - - - - - renderBlock('choice_widget') ?> - +.. code-block:: html+twig + + {# app/Resources/views/form/fields.html.twig #} + {% block shipping_widget %} + {% spaceless %} + {% if expanded %} + + {% else %} + {# just let the choice widget render the select tag #} + {{ block('choice_widget') }} + {% endif %} + {% endspaceless %} + {% endblock %} .. tip:: diff --git a/form/create_form_type_extension.rst b/form/create_form_type_extension.rst index 5030cc63b9d..93fd0d95bed 100644 --- a/form/create_form_type_extension.rst +++ b/form/create_form_type_extension.rst @@ -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 %} - - {% 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 %} + + {% endif %} - - widget($form) ?> - - - + {% endspaceless %} + {% endblock %} .. note:: diff --git a/form/dynamic_form_modification.rst b/form/dynamic_form_modification.rst index 22b5cded111..24e4ecd93db 100644 --- a/form/dynamic_form_modification.rst +++ b/form/dynamic_form_modification.rst @@ -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) }} {# + 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. + } + }); + }); + 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 diff --git a/form/embedded.rst b/form/embedded.rst index 164c3547e56..3f3d0b5937b 100644 --- a/form/embedded.rst +++ b/form/embedded.rst @@ -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 + {# ... #} - {# ... #} +

Category

+
+ {{ form_row(form.category.name) }} +
-

Category

-
- {{ form_row(form.category.name) }} -
- - {# ... #} - - .. code-block:: html+php - - - -

Category

-
- row($form['category']['name']) ?> -
- - + {# ... #} 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 diff --git a/form/form_collections.rst b/form/form_collections.rst index 3e4d016ef43..ac710b1c575 100644 --- a/form/form_collections.rst +++ b/form/form_collections.rst @@ -190,48 +190,26 @@ that are already related to this ``Task``. In the above controller, I added some dummy code so that you can see this in action (since a ``Task`` has zero tags when first created). -.. configuration-block:: +.. code-block:: html+twig - .. code-block:: html+twig - - {# app/Resources/views/task/new.html.twig #} - - {# ... #} - - {{ form_start(form) }} - {# render the task's only field: description #} - {{ form_row(form.description) }} - -

Tags

- - {{ form_end(form) }} - - {# ... #} - - .. code-block:: html+php - - + {# app/Resources/views/task/new.html.twig #} - + {# ... #} - start($form) ?> - - row($form['description']) ?> + {{ form_start(form) }} + {# render the task's only field: description #} + {{ form_row(form.description) }} -

Tags

- - end($form) ?> +

Tags

+ + {{ form_end(form) }} - + {# ... #} When the user submits the form, the submitted data for the ``tags`` field are used to construct an ``ArrayCollection`` of ``Tag`` objects, which is then set @@ -297,21 +275,11 @@ In addition to telling the field to accept any number of submitted objects, the is a little "template" that contains all the HTML to be able to render any new "tag" forms. To render it, make the following change to your template: -.. configuration-block:: - - .. code-block:: html+twig +.. code-block:: html+twig - - - .. code-block:: html+php - - + .. note:: diff --git a/form/form_customization.rst b/form/form_customization.rst index e26f1b93075..be20db2005a 100644 --- a/form/form_customization.rst +++ b/form/form_customization.rst @@ -16,35 +16,19 @@ Recall that the label, error and HTML widget of a form field can easily be rendered by using the ``form_row()`` Twig function or the ``row`` PHP helper method: -.. configuration-block:: - - .. code-block:: twig - - {{ form_row(form.age) }} - - .. code-block:: php +.. code-block:: twig - row($form['age']); ?> + {{ form_row(form.age) }} You can also render each of the three parts of the field individually: -.. configuration-block:: - - .. code-block:: html+twig - -
- {{ form_label(form.age) }} - {{ form_errors(form.age) }} - {{ form_widget(form.age) }} -
- - .. code-block:: php +.. code-block:: html+twig -
- label($form['age']); ?> - errors($form['age']); ?> - widget($form['age']); ?> -
+
+ {{ form_label(form.age) }} + {{ form_errors(form.age) }} + {{ form_widget(form.age) }} +
In both cases, the form label, errors and HTML widget are rendered by using a set of markup that ships standard with Symfony. For example, both of the @@ -63,23 +47,13 @@ above templates would render: To quickly prototype and test a form, you can render the entire form with just one line: -.. configuration-block:: - - .. code-block:: twig - - {# renders all fields #} - {{ form_widget(form) }} - - {# renders all fields *and* the form start and end tags #} - {{ form(form) }} - - .. code-block:: php +.. code-block:: twig - - widget($form) ?> + {# renders all fields #} + {{ form_widget(form) }} - - form($form) ?> + {# renders all fields *and* the form start and end tags #} + {{ form(form) }} The remainder of this recipe will explain how every part of the form's markup can be modified at several different levels. For more information about form @@ -129,15 +103,9 @@ some or all of its fragments. For example, when the widget of an ``integer`` type field is rendered, an ``input`` ``number`` field is generated -.. configuration-block:: - - .. code-block:: html+twig - - {{ form_widget(form.age) }} - - .. code-block:: php +.. code-block:: html+twig - widget($form['age']) ?> + {{ form_widget(form.age) }} renders: @@ -157,41 +125,23 @@ the ``FrameworkBundle/Resources/views/Form`` folder. The default implementation of the ``integer_widget`` fragment looks like this: -.. configuration-block:: - - .. code-block:: twig - - {# form_div_layout.html.twig #} - {% block integer_widget %} - {% set type = type|default('number') %} - {{ block('form_widget_simple') }} - {% endblock integer_widget %} - - .. code-block:: html+php +.. code-block:: twig - - block($form, 'form_widget_simple', array('type' => isset($type) ? $type : "number")) ?> + {# form_div_layout.html.twig #} + {% block integer_widget %} + {% set type = type|default('number') %} + {{ block('form_widget_simple') }} + {% endblock integer_widget %} As you can see, this fragment itself renders another fragment - ``form_widget_simple``: -.. configuration-block:: - - .. code-block:: html+twig - - {# form_div_layout.html.twig #} - {% block form_widget_simple %} - {% set type = type|default('text') %} - - {% endblock form_widget_simple %} - - .. code-block:: html+php +.. code-block:: html+twig - - value="escape($value) ?>" - block($form, 'widget_attributes') ?> - /> + {# form_div_layout.html.twig #} + {% block form_widget_simple %} + {% set type = type|default('text') %} + + {% endblock form_widget_simple %} The point is, the fragments dictate the HTML output of each part of a form. To customize the form output, you just need to identify and override the correct @@ -692,31 +642,17 @@ accomplished by customizing a fragment whose name is a combination of the field' ``id`` attribute and which part of the field is being customized. For example, to customize the ``name`` field only: -.. configuration-block:: - - .. code-block:: html+twig - - {% form_theme form _self %} - - {% block _product_name_widget %} -
- {{ block('form_widget_simple') }} -
- {% endblock %} - - {{ form_widget(form.name) }} - - .. code-block:: html+php - - - setTheme($form, array(':form')); ?> +.. code-block:: html+twig - widget($form['name']); ?> + {% form_theme form _self %} - + {% block _product_name_widget %}
- block('form_widget_simple') ?> + {{ block('form_widget_simple') }}
+ {% endblock %} + + {{ form_widget(form.name) }} Here, the ``_product_name_widget`` fragment defines the template to use for the field whose *id* is ``product_name`` (and name is ``product[name]``). @@ -748,35 +684,19 @@ field whose *id* is ``product_name`` (and name is ``product[name]``). You can also override the markup for an entire field row using the same method: -.. configuration-block:: - - .. code-block:: html+twig - - {% form_theme form _self %} - - {% block _product_name_row %} -
- {{ form_label(form) }} - {{ form_errors(form) }} - {{ form_widget(form) }} -
- {% endblock %} - - {{ form_row(form.name) }} - - .. code-block:: html+php - - - setTheme($form, array(':form')); ?> +.. code-block:: html+twig - row($form['name']); ?> + {% form_theme form _self %} - + {% block _product_name_row %}
- label($form) ?> - errors($form) ?> - widget($form) ?> + {{ form_label(form) }} + {{ form_errors(form) }} + {{ form_widget(form) }}
+ {% endblock %} + + {{ form_row(form.name) }} .. _form-custom-prototype: @@ -788,26 +708,16 @@ the prototype can be overridden with a completely custom prototype by overriding a block. For example, if your form field is named ``tasks``, you will be able to change the widget for each task as follows: -.. configuration-block:: - - .. code-block:: html+twig - - {% form_theme form _self %} - - {% block _tasks_entry_widget %} - - {{ form_widget(form.task) }} - {{ form_widget(form.dueDate) }} - - {% endblock %} +.. code-block:: html+twig - .. code-block:: html+php + {% form_theme form _self %} - + {% block _tasks_entry_widget %} - widget($form->task) ?> - widget($form->dueDate) ?> + {{ form_widget(form.task) }} + {{ form_widget(form.dueDate) }} + {% endblock %} Not only can you override the rendered widget, but you can also change the complete form row or the label as well. For the ``tasks`` field given above, @@ -847,15 +757,9 @@ There are many different ways to customize how errors are rendered when a form is submitted with errors. The error messages for a field are rendered when you use the ``form_errors()`` helper: -.. configuration-block:: - - .. code-block:: twig - - {{ form_errors(form.age) }} - - .. code-block:: php +.. code-block:: twig - errors($form['age']); ?> + {{ form_errors(form.age) }} By default, the errors are rendered inside an unordered list: @@ -868,35 +772,22 @@ By default, the errors are rendered inside an unordered list: To override how errors are rendered for *all* fields, simply copy, paste and customize the ``form_errors`` fragment. -.. configuration-block:: - - .. code-block:: html+twig - - {% form_theme form _self %} - - {# form_errors.html.twig #} - {% block form_errors %} - {% spaceless %} - {% if errors|length > 0 %} - - {% endif %} - {% endspaceless %} - {% endblock form_errors %} +.. code-block:: html+twig - .. code-block:: html+php + {% form_theme form _self %} - - + {# form_errors.html.twig #} + {% block form_errors %} + {% spaceless %} + {% if errors|length > 0 %} - + {% endif %} + {% endspaceless %} + {% endblock form_errors %} .. tip:: @@ -915,58 +806,35 @@ of PHP templates). For example: ``text_errors`` (or ``text_errors.html.php``). Certain errors that are more global to your form (i.e. not specific to just one field) are rendered separately, usually at the top of your form: -.. configuration-block:: - - .. code-block:: twig - - {{ form_errors(form) }} - - .. code-block:: php +.. code-block:: twig - render($form); ?> + {{ form_errors(form) }} To customize *only* the markup used for these errors, follow the same directions as above, but now check if the ``compound`` variable is set to ``true``. If it is ``true``, it means that what's being currently rendered is a collection of fields (e.g. a whole form), and not just an individual field. -.. configuration-block:: +.. code-block:: html+twig + + {% form_theme form _self %} - .. code-block:: html+twig - - {% form_theme form _self %} - - {# form_errors.html.twig #} - {% block form_errors %} - {% spaceless %} - {% if errors|length > 0 %} - {% if compound %} - - {% else %} - {# ... display the errors for a single field #} - {% endif %} + {# form_errors.html.twig #} + {% block form_errors %} + {% spaceless %} + {% if errors|length > 0 %} + {% if compound %} + + {% else %} + {# ... display the errors for a single field #} {% endif %} - {% endspaceless %} - {% endblock form_errors %} - - .. code-block:: html+php - - - - - - - - - + {% endif %} + {% endspaceless %} + {% endblock form_errors %} Customizing the "Form Row" ~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -977,27 +845,16 @@ a field. To customize the markup used for rendering *all* form field rows, override the ``form_row`` fragment. For example, suppose you want to add a class to the ``div`` element around each row: -.. configuration-block:: - - .. code-block:: html+twig - - {# form_row.html.twig #} - {% block form_row %} -
- {{ form_label(form) }} - {{ form_errors(form) }} - {{ form_widget(form) }} -
- {% endblock form_row %} - - .. code-block:: html+php +.. code-block:: html+twig - + {# form_row.html.twig #} + {% block form_row %}
- label($form) ?> - errors($form) ?> - widget($form) ?> + {{ form_label(form) }} + {{ form_errors(form) }} + {{ form_widget(form) }}
+ {% endblock form_row %} .. tip:: @@ -1128,15 +985,9 @@ original template: To render a help message below a field, pass in a ``help`` variable: -.. configuration-block:: - - .. code-block:: twig - - {{ form_widget(form.title, {'help': 'foobar'}) }} - - .. code-block:: php +.. code-block:: twig - widget($form['title'], array('help' => 'foobar')) ?> + {{ form_widget(form.title, {'help': 'foobar'}) }} .. tip:: @@ -1149,21 +1000,10 @@ Most of the functions available for rendering different parts of a form (e.g. the form widget, form label, form errors, etc.) also allow you to make certain customizations directly. Look at the following example: -.. configuration-block:: - - .. code-block:: twig - - {# render a widget, but add a "foo" class to it #} - {{ form_widget(form.name, { 'attr': {'class': 'foo'} }) }} - - .. code-block:: php +.. code-block:: twig - - widget($form['name'], array( - 'attr' => array( - 'class' => 'foo', - ), - )) ?> + {# render a widget, but add a "foo" class to it #} + {{ form_widget(form.name, { 'attr': {'class': 'foo'} }) }} The array passed as the second argument contains form "variables". For more details about this concept in Twig, see :ref:`twig-reference-form-variables`. diff --git a/form/form_themes.rst b/form/form_themes.rst index f580c278657..c312b586298 100644 --- a/form/form_themes.rst +++ b/form/form_themes.rst @@ -24,56 +24,33 @@ To understand how this works, customize the ``form_row`` fragment and add a class attribute to the ``div`` element that surrounds each row. To do this, create a new template file that will store the new markup: -.. configuration-block:: - - .. code-block:: html+twig - - {# app/Resources/views/form/fields.html.twig #} - {% block form_row %} - {% spaceless %} -
- {{ form_label(form) }} - {{ form_errors(form) }} - {{ form_widget(form) }} -
- {% endspaceless %} - {% endblock form_row %} - - .. code-block:: html+php +.. code-block:: html+twig - + {# app/Resources/views/form/fields.html.twig #} + {% block form_row %} + {% spaceless %}
- label($form, $label) ?> - errors($form) ?> - widget($form, $parameters) ?> + {{ form_label(form) }} + {{ form_errors(form) }} + {{ form_widget(form) }}
+ {% endspaceless %} + {% endblock form_row %} The ``form_row`` form fragment is used when rendering most fields via the ``form_row()`` function. To tell the Form component to use your new ``form_row`` fragment defined above, add the following to the top of the template that renders the form: -.. configuration-block:: - - .. code-block:: html+twig - - {# app/Resources/views/default/new.html.twig #} - {% form_theme form 'form/fields.html.twig' %} - - {# or if you want to use multiple themes #} - {% form_theme form 'form/fields.html.twig' 'form/fields2.html.twig' %} - - {# ... render the form #} - - .. code-block:: html+php +.. code-block:: html+twig - - setTheme($form, array('form')) ?> + {# app/Resources/views/default/new.html.twig #} + {% form_theme form 'form/fields.html.twig' %} - - setTheme($form, array('form', 'form2')) ?> + {# or if you want to use multiple themes #} + {% form_theme form 'form/fields.html.twig' 'form/fields2.html.twig' %} - + {# ... render the form #} The ``form_theme`` tag (in Twig) "imports" the fragments defined in the given template and uses them when rendering the form. In other words, when the diff --git a/form/rendering.rst b/form/rendering.rst index c6b6ecbf83d..dd9e0b7e614 100644 --- a/form/rendering.rst +++ b/form/rendering.rst @@ -7,27 +7,15 @@ How to Control the Rendering of a Form So far, you've seen how an entire form can be rendered with just one line of code. Of course, you'll usually need much more flexibility when rendering: -.. configuration-block:: +.. code-block:: html+twig - .. code-block:: html+twig + {# app/Resources/views/default/new.html.twig #} + {{ form_start(form) }} + {{ form_errors(form) }} - {# app/Resources/views/default/new.html.twig #} - {{ form_start(form) }} - {{ form_errors(form) }} - - {{ form_row(form.task) }} - {{ form_row(form.dueDate) }} - {{ form_end(form) }} - - .. code-block:: html+php - - - start($form) ?> - errors($form) ?> - - row($form['task']) ?> - row($form['dueDate']) ?> - end($form) ?> + {{ form_row(form.task) }} + {{ form_row(form.dueDate) }} + {{ form_end(form) }} You already know the ``form_start()`` and ``form_end()`` functions, but what do the other functions do? @@ -49,15 +37,9 @@ output can be customized on many different levels. You can access the current data of your form via ``form.vars.value``: - .. configuration-block:: - - .. code-block:: twig - - {{ form.vars.value.task }} - - .. code-block:: html+php +.. code-block:: twig - vars['value']->getTask() ?> + {{ form.vars.value.task }} .. index:: single: Forms; Rendering each field by hand @@ -71,67 +53,35 @@ well). But since life isn't always so simple, you can also render each field entirely by hand. The end-product of the following is the same as when you used the ``form_row()`` helper: -.. configuration-block:: - - .. code-block:: html+twig - - {{ form_start(form) }} - {{ form_errors(form) }} - -
- {{ form_label(form.task) }} - {{ form_errors(form.task) }} - {{ form_widget(form.task) }} -
- -
- {{ form_label(form.dueDate) }} - {{ form_errors(form.dueDate) }} - {{ form_widget(form.dueDate) }} -
- -
- {{ form_widget(form.save) }} -
- - {{ form_end(form) }} - - .. code-block:: html+php +.. code-block:: html+twig - start($form) ?> + {{ form_start(form) }} + {{ form_errors(form) }} - errors($form) ?> +
+ {{ form_label(form.task) }} + {{ form_errors(form.task) }} + {{ form_widget(form.task) }} +
-
- label($form['task']) ?> - errors($form['task']) ?> - widget($form['task']) ?> -
+
+ {{ form_label(form.dueDate) }} + {{ form_errors(form.dueDate) }} + {{ form_widget(form.dueDate) }} +
-
- label($form['dueDate']) ?> - errors($form['dueDate']) ?> - widget($form['dueDate']) ?> -
+
+ {{ form_widget(form.save) }} +
-
- widget($form['save']) ?> -
- - end($form) ?> + {{ form_end(form) }} If the auto-generated label for a field isn't quite right, you can explicitly specify it: -.. configuration-block:: - - .. code-block:: html+twig - - {{ form_label(form.task, 'Task Description') }} - - .. code-block:: html+php +.. code-block:: html+twig - label($form['task'], 'Task Description') ?> + {{ form_label(form.task, 'Task Description') }} Some field types have additional rendering options that can be passed to the widget. These options are documented with each type, but one common @@ -139,44 +89,24 @@ option is ``attr``, which allows you to modify attributes on the form element. The following would add the ``task_field`` class to the rendered input text field: -.. configuration-block:: - - .. code-block:: html+twig - - {{ form_widget(form.task, {'attr': {'class': 'task_field'}}) }} - - .. code-block:: html+php +.. code-block:: html+twig - widget($form['task'], array( - 'attr' => array('class' => 'task_field'), - )) ?> + {{ form_widget(form.task, {'attr': {'class': 'task_field'}}) }} If you need to render form fields "by hand" then you can access individual values for fields such as the ``id``, ``name`` and ``label``. For example to get the ``id``: -.. configuration-block:: +.. code-block:: html+twig - .. code-block:: html+twig - - {{ form.task.vars.id }} - - .. code-block:: html+php - - vars['id']?> + {{ form.task.vars.id }} To get the value used for the form field's name attribute you need to use the ``full_name`` value: -.. configuration-block:: - - .. code-block:: html+twig - - {{ form.task.vars.full_name }} - - .. code-block:: html+php +.. code-block:: html+twig - vars['full_name'] ?> + {{ form.task.vars.full_name }} Twig Template Function Reference -------------------------------- diff --git a/http_cache/esi.rst b/http_cache/esi.rst index 8e4f0d452e6..9e818115b66 100644 --- a/http_cache/esi.rst +++ b/http_cache/esi.rst @@ -120,41 +120,15 @@ for more details). As the embedded content comes from another page (or controller for that matter), Symfony uses the standard ``render`` helper to configure ESI tags: -.. configuration-block:: - - .. code-block:: twig - - {# app/Resources/views/static/about.html.twig #} - - {# you can use a controller reference #} - {{ render_esi(controller('AppBundle:News:latest', { 'maxPerPage': 5 })) }} - - {# ... or a URL #} - {{ render_esi(url('latest_news', { 'maxPerPage': 5 })) }} - - .. code-block:: html+php +.. code-block:: twig - + {# app/Resources/views/static/about.html.twig #} - - render( - new Symfony\Component\HttpKernel\Controller\ControllerReference( - 'AppBundle:News:latest', - array('maxPerPage' => 5) - ), - array('strategy' => 'esi') - ) ?> + {# you can use a controller reference #} + {{ render_esi(controller('AppBundle:News:latest', { 'maxPerPage': 5 })) }} - - render( - // The url() method was introduced in Symfony 2.8. Prior to 2.8, - // you had to use generate($name, $parameters, UrlGeneratorInterface::ABSOLUTE_URL) - $view['router']->path( - 'latest_news', - array('maxPerPage' => 5) - ), - array('strategy' => 'esi') - ) ?> + {# ... or a URL #} + {{ render_esi(url('latest_news', { 'maxPerPage': 5 })) }} By using the ``esi`` renderer (via the ``render_esi()`` Twig function), you tell Symfony that the action should be rendered as an ESI tag. You might be diff --git a/routing/generate_url_javascript.rst b/routing/generate_url_javascript.rst index a748d1c99db..8414a928800 100644 --- a/routing/generate_url_javascript.rst +++ b/routing/generate_url_javascript.rst @@ -4,24 +4,11 @@ How to Generate Routing URLs in JavaScript If you're in a Twig template, you can use the same ``path()`` function to set JavaScript variables. The ``escape()`` function helps escape any non-JavaScript-safe values: -.. configuration-block:: - - .. code-block:: html+twig - - - - .. code-block:: html+php +.. code-block:: html+twig - + .. versionadded:: 2.8 The ``path()`` PHP templating helper was introduced in Symfony 2.8. Prior diff --git a/security/csrf_in_login_form.rst b/security/csrf_in_login_form.rst index 67ac50c5f6d..c5a7452f31d 100644 --- a/security/csrf_in_login_form.rst +++ b/security/csrf_in_login_form.rst @@ -120,39 +120,20 @@ token, which can be generated by using the ``csrf_token()`` function. That function requires a token ID, which must be set to ``authenticate`` when using the login form: -.. configuration-block:: - - .. code-block:: html+twig - - {# src/AppBundle/Resources/views/Security/login.html.twig #} - - {# ... #} -
- {# ... the login fields #} - - - - -
- - .. code-block:: html+php +.. code-block:: html+twig - + {# src/AppBundle/Resources/views/Security/login.html.twig #} - - -
- + {# ... #} + + {# ... the login fields #} - + - -
+ + After this, you have protected your login form against CSRF attacks. diff --git a/security/form_login.rst b/security/form_login.rst index 89945ec5309..d813d0bde00 100644 --- a/security/form_login.rst +++ b/security/form_login.rst @@ -164,27 +164,15 @@ Defining the redirect URL via GET using a query string parameter: Defining the redirect URL via POST using a hidden form field: -.. configuration-block:: - - .. code-block:: html+twig +.. code-block:: html+twig - {# app/Resources/views/security/login.html.twig #} -
- {# ... #} + {# app/Resources/views/security/login.html.twig #} + + {# ... #} - - -
- - .. code-block:: html+php - - -
- // ... - - - -
+ + + Using the Referring URL ~~~~~~~~~~~~~~~~~~~~~~~ @@ -318,27 +306,15 @@ This option can also be set via the ``_failure_path`` request parameter: http://example.com/some/path?_failure_path=/forgot-password -.. configuration-block:: - - .. code-block:: html+twig - - {# app/Resources/views/security/login.html.twig #} -
- {# ... #} - - - -
- - .. code-block:: html+php +.. code-block:: html+twig - -
- + {# app/Resources/views/security/login.html.twig #} + + {# ... #} - - -
+ + + Customizing the Target and Failure Request Parameters ----------------------------------------------------- @@ -407,26 +383,15 @@ are now fully customized: http://example.com/some/path?go_to=/dashboard&back_to=/forgot-password -.. configuration-block:: - - .. code-block:: html+twig - - {# app/Resources/views/security/login.html.twig #} -
- {# ... #} +.. code-block:: html+twig - - - -
+ {# app/Resources/views/security/login.html.twig #} +
+ {# ... #} - .. code-block:: html+php + + + +
- -
- - - - -
diff --git a/security/form_login_setup.rst b/security/form_login_setup.rst index 65a566060f1..6b762efc142 100644 --- a/security/form_login_setup.rst +++ b/security/form_login_setup.rst @@ -170,57 +170,30 @@ the submitted username and password and authenticating the user. Finally, create the template: -.. configuration-block:: - - .. code-block:: html+twig - - {# app/Resources/views/security/login.html.twig #} - {# ... you will probably extend your base template, like base.html.twig #} - - {% if error %} -
{{ error.messageKey|trans(error.messageData, 'security') }}
- {% endif %} - -
- - - - - - - {# - If you want to control the URL the user - is redirected to on success (more details below) - - #} - - -
+.. code-block:: html+twig - .. code-block:: html+php + {# app/Resources/views/security/login.html.twig #} + {# ... you will probably extend your base template, like base.html.twig #} - - -
getMessage() ?>
- + {% if error %} +
{{ error.messageKey|trans(error.messageData, 'security') }}
+ {% endif %} - -
- - + + + - - + + - + {# + If you want to control the URL the user + is redirected to on success (more details below) + + #} - -
+ + .. tip:: diff --git a/security/impersonating_user.rst b/security/impersonating_user.rst index 89ba9dba216..afc8483d7a9 100644 --- a/security/impersonating_user.rst +++ b/security/impersonating_user.rst @@ -83,25 +83,11 @@ During impersonation, the user is provided with a special role called ``ROLE_PREVIOUS_ADMIN``. In a template, for instance, this role can be used to show a link to exit impersonation: -.. configuration-block:: - - .. code-block:: html+twig - - {% if is_granted('ROLE_PREVIOUS_ADMIN') %} - Exit impersonation - {% endif %} - - .. code-block:: html+php +.. code-block:: html+twig - isGranted('ROLE_PREVIOUS_ADMIN')): ?> - - - Exit impersonation - - + {% if is_granted('ROLE_PREVIOUS_ADMIN') %} + Exit impersonation + {% endif %} In some cases you may need to get the object that represents the impersonator user rather than the impersonated user. Use the following snippet to iterate diff --git a/security/remember_me.rst b/security/remember_me.rst index 145e18d7f8e..9966c1f27a0 100644 --- a/security/remember_me.rst +++ b/security/remember_me.rst @@ -147,50 +147,25 @@ the cookie will automatically be set when the checkbox is checked and the user successfully logs in. So, your specific login form might ultimately look like this: -.. configuration-block:: - - .. code-block:: html+twig - - {# app/Resources/views/security/login.html.twig #} - {% if error %} -
{{ error.message }}
- {% endif %} - -
- - - - - - - - - - -
- - .. code-block:: html+php +.. code-block:: html+twig - - -
getMessage() ?>
- + {# app/Resources/views/security/login.html.twig #} + {% if error %} +
{{ error.message }}
+ {% endif %} - -
- - + + + - - + + - - + + - -
+ + The user will then automatically be logged in on subsequent visits while the cookie remains valid. diff --git a/templating/app_variable.rst b/templating/app_variable.rst index a9843f84b03..6fdc00ec02b 100644 --- a/templating/app_variable.rst +++ b/templating/app_variable.rst @@ -30,23 +30,13 @@ automatically: ``app.debug`` True if in debug mode. False otherwise. -.. configuration-block:: +.. code-block:: html+twig - .. code-block:: html+twig - -

Username: {{ app.user.username }}

- {% if app.debug %} -

Request method: {{ app.request.method }}

-

Application Environment: {{ app.environment }}

- {% endif %} - - .. code-block:: html+php - -

Username: getUser()->getUsername() ?>

- getDebug()): ?> -

Request method: getRequest()->getMethod() ?>

-

Application Environment: getEnvironment() ?>

- +

Username: {{ app.user.username }}

+ {% if app.debug %} +

Request method: {{ app.request.method }}

+

Application Environment: {{ app.environment }}

+ {% endif %} .. tip:: diff --git a/templating/embedding_controllers.rst b/templating/embedding_controllers.rst index 61ff3fd1a65..c251b51e2ee 100644 --- a/templating/embedding_controllers.rst +++ b/templating/embedding_controllers.rst @@ -43,58 +43,29 @@ First, create a controller that renders a certain number of recent articles:: Then, create a ``recent_list`` template fragment to list the articles given by the controller: -.. configuration-block:: +.. code-block:: html+twig - .. code-block:: html+twig - - {# app/Resources/views/article/recent_list.html.twig #} - {% for article in articles %} - - {{ article.title }} - - {% endfor %} - - .. code-block:: html+php - - - - - getTitle() ?> - - + {# app/Resources/views/article/recent_list.html.twig #} + {% for article in articles %} + + {{ article.title }} + + {% endfor %} Finally, call the controller from any template using the ``render()`` function and the common syntax for controllers (i.e. **bundle**:**controller**:**action**): -.. configuration-block:: - - .. code-block:: html+twig - - {# app/Resources/views/base.html.twig #} - - {# ... #} - - - .. code-block:: html+php +.. code-block:: html+twig - + {# app/Resources/views/base.html.twig #} - - + {# ... #} + Whenever you find that you need a variable or a piece of information that you don't have access to in a template, consider rendering a controller. diff --git a/templating/escaping.rst b/templating/escaping.rst index 1cdfa7e81b2..fce167b9a01 100644 --- a/templating/escaping.rst +++ b/templating/escaping.rst @@ -10,15 +10,9 @@ is that dynamic content could break the HTML of the resulting page or allow a malicious user to perform a `Cross Site Scripting`_ (XSS) attack. Consider this classic example: -.. configuration-block:: +.. code-block:: html+twig - .. code-block:: html+twig - - Hello {{ name }} - - .. code-block:: html+php - - Hello + Hello {{ name }} Imagine the user enters the following code for their name: diff --git a/templating/formats.rst b/templating/formats.rst index 6e9b8f38162..d8dd47bc565 100644 --- a/templating/formats.rst +++ b/templating/formats.rst @@ -61,24 +61,11 @@ special ``_format`` placeholder in your route definition:: Now, include the ``_format`` placeholder when generating a route for another format: -.. configuration-block:: +.. code-block:: html+twig - .. code-block:: html+twig - - - View as XML - - - .. code-block:: html+php - - - - View as XML - + + View as XML + .. seealso:: diff --git a/templating/hinclude.rst b/templating/hinclude.rst index ca4746e1f1d..d9a77e2c8eb 100644 --- a/templating/hinclude.rst +++ b/templating/hinclude.rst @@ -9,27 +9,10 @@ As the embedded content comes from another page (or controller for that matter), Symfony uses a version of the standard ``render()`` function to configure ``hinclude`` tags: -.. configuration-block:: - - .. code-block:: twig - - {{ render_hinclude(controller('...')) }} - {{ render_hinclude(url('...')) }} - - .. code-block:: php - - render( - new ControllerReference('...'), - array('renderer' => 'hinclude') - ) ?> +.. code-block:: twig - - render( - $view['router']->url('...'), - array('renderer' => 'hinclude') - ) ?> + {{ render_hinclude(controller('...')) }} + {{ render_hinclude(url('...')) }} .. note:: @@ -119,40 +102,16 @@ in your application configuration: You can define default templates per ``render()`` function (which will override any global default template that is defined): -.. configuration-block:: - - .. code-block:: twig +.. code-block:: twig - {{ render_hinclude(controller('...'), { - 'default': 'default/content.html.twig' - }) }} - - .. code-block:: php - - render( - new ControllerReference('...'), - array( - 'renderer' => 'hinclude', - 'default' => 'default/content.html.twig', - ) - ) ?> + {{ render_hinclude(controller('...'), { + 'default': 'default/content.html.twig' + }) }} Or you can also specify a string to display as the default content: -.. configuration-block:: - - .. code-block:: twig - - {{ render_hinclude(controller('...'), {'default': 'Loading...'}) }} - - .. code-block:: php +.. code-block:: twig - render( - new ControllerReference('...'), - array( - 'renderer' => 'hinclude', - 'default' => 'Loading...', - ) - ) ?> + {{ render_hinclude(controller('...'), {'default': 'Loading...'}) }} .. _`hinclude.js`: http://mnot.github.io/hinclude/ diff --git a/templating/render_without_controller.rst b/templating/render_without_controller.rst index 8d9fb57783f..37c7967b4d0 100644 --- a/templating/render_without_controller.rst +++ b/templating/render_without_controller.rst @@ -59,24 +59,9 @@ within a template is typically to prepare some data in a custom controller, this is probably only useful if you'd like to cache this page partial (see :ref:`templating-no-controller-caching`). -.. configuration-block:: - - .. code-block:: html+twig - - {{ render(url('acme_privacy')) }} - - .. code-block:: html+php - - +.. code-block:: html+twig - - render( - $view['router']->url('acme_privacy', array()) - ) ?> + {{ render(url('acme_privacy')) }} .. _templating-no-controller-caching: