diff --git a/controller.rst b/controller.rst index 1718c3866af..70b6ed7f290 100644 --- a/controller.rst +++ b/controller.rst @@ -417,47 +417,25 @@ you'll use this key to retrieve the message. In the template of the next page (or even better, in your base layout template), read any flash messages from the session: -.. configuration-block:: +.. code-block:: html+twig - .. code-block:: html+twig + {# app/Resources/views/base.html.twig #} - {# app/Resources/views/base.html.twig #} + {# you can read and display just one flash message type... #} + {% for flash_message in app.session.flashBag.get('notice') %} +
+ {{ flash_message }} +
+ {% endfor %} - {# you can read and display just one flash message type... #} - {% for flash_message in app.session.flashBag.get('notice') %} -
+ {# ...or you can read and display every flash message available #} + {% for type, flash_messages in app.session.flashBag.all %} + {% for flash_message in flash_messages %} +
{{ flash_message }}
{% endfor %} - - {# ...or you can read and display every flash message available #} - {% for type, flash_messages in app.session.flashBag.all %} - {% for flash_message in flash_messages %} -
- {{ flash_message }} -
- {% endfor %} - {% endfor %} - - .. code-block:: html+php - - - - // you can read and display just one flash message type... - getFlashBag()->get('notice') as $message): ?> -
- -
- - - // ...or you can read and display every flash message available - getFlashBag()->all() as $type => $flash_messages): ?> - -
- -
- - + {% endfor %} .. note:: diff --git a/forms.rst b/forms.rst index 21b07ce48a1..7f43ab2fa86 100644 --- a/forms.rst +++ b/forms.rst @@ -152,21 +152,12 @@ done by passing a special form "view" object to your template (notice the ``$form->createView()`` in the controller above) and using a set of form helper functions: -.. configuration-block:: - - .. code-block:: html+twig - - {# app/Resources/views/default/new.html.twig #} - {{ form_start(form) }} - {{ form_widget(form) }} - {{ form_end(form) }} - - .. code-block:: html+php +.. code-block:: html+twig - - start($form) ?> - widget($form) ?> - end($form) ?> + {# app/Resources/views/default/new.html.twig #} + {{ form_start(form) }} + {{ form_widget(form) }} + {{ form_end(form) }} .. image:: /_images/form/simple-form.png :align: center @@ -432,21 +423,12 @@ Validation is a very powerful feature of Symfony and has its own but are being prevented by your browser from, for example, submitting blank fields. - .. configuration-block:: - - .. code-block:: html+twig - - {# app/Resources/views/default/new.html.twig #} - {{ form_start(form, {'attr': {'novalidate': 'novalidate'}}) }} - {{ form_widget(form) }} - {{ form_end(form) }} - - .. code-block:: html+php +.. code-block:: html+twig - - start($form, array('attr' => array('novalidate' => 'novalidate') ?> - widget($form) ?> - end($form) ?> + {# app/Resources/views/default/new.html.twig #} + {{ form_start(form, {'attr': {'novalidate': 'novalidate'}}) }} + {{ form_widget(form) }} + {{ form_end(form) }} .. index:: single: Forms; Built-in field types diff --git a/frontend/assetic/apply_to_option.rst b/frontend/assetic/apply_to_option.rst index a144f08172f..02507a8028f 100644 --- a/frontend/assetic/apply_to_option.rst +++ b/frontend/assetic/apply_to_option.rst @@ -67,22 +67,11 @@ Filter a single File You can now serve up a single CoffeeScript file as JavaScript from within your templates: -.. configuration-block:: - - .. code-block:: html+twig - - {% javascripts '@AppBundle/Resources/public/js/example.coffee' filter='coffee' %} - - {% endjavascripts %} +.. code-block:: html+twig - .. code-block:: html+php - - javascripts( - array('@AppBundle/Resources/public/js/example.coffee'), - array('coffee') - ) as $url): ?> - - + {% javascripts '@AppBundle/Resources/public/js/example.coffee' filter='coffee' %} + + {% endjavascripts %} This is all that's needed to compile this CoffeeScript file and serve it as the compiled JavaScript. @@ -92,27 +81,13 @@ Filter multiple Files You can also combine multiple CoffeeScript files into a single output file: -.. configuration-block:: - - .. code-block:: html+twig +.. code-block:: html+twig - {% javascripts '@AppBundle/Resources/public/js/example.coffee' - '@AppBundle/Resources/public/js/another.coffee' - filter='coffee' %} - - {% endjavascripts %} - - .. code-block:: html+php - - javascripts( - array( - '@AppBundle/Resources/public/js/example.coffee', - '@AppBundle/Resources/public/js/another.coffee', - ), - array('coffee') - ) as $url): ?> - - + {% javascripts '@AppBundle/Resources/public/js/example.coffee' + '@AppBundle/Resources/public/js/another.coffee' + filter='coffee' %} + + {% endjavascripts %} Both files will now be served up as a single file compiled into regular JavaScript. @@ -188,24 +163,12 @@ template. You can also list regular JavaScript files, all of which will be combined and rendered as a single JavaScript file (with only the ``.coffee`` files being run through the CoffeeScript filter): -.. configuration-block:: +.. code-block:: html+twig + + {% javascripts '@AppBundle/Resources/public/js/example.coffee' + '@AppBundle/Resources/public/js/another.coffee' + '@AppBundle/Resources/public/js/regular.js' %} + + {% endjavascripts %} + - .. code-block:: html+twig - - {% javascripts '@AppBundle/Resources/public/js/example.coffee' - '@AppBundle/Resources/public/js/another.coffee' - '@AppBundle/Resources/public/js/regular.js' %} - - {% endjavascripts %} - - .. code-block:: html+php - - javascripts( - array( - '@AppBundle/Resources/public/js/example.coffee', - '@AppBundle/Resources/public/js/another.coffee', - '@AppBundle/Resources/public/js/regular.js', - ) - ) as $url): ?> - - diff --git a/frontend/assetic/asset_management.rst b/frontend/assetic/asset_management.rst index 2706f4e3040..80d442b798c 100644 --- a/frontend/assetic/asset_management.rst +++ b/frontend/assetic/asset_management.rst @@ -97,15 +97,9 @@ to the user. Without Assetic, you just serve the files that are stored in the application directly: -.. configuration-block:: - - .. code-block:: html+twig +.. code-block:: html+twig - - - .. code-block:: php - - + But *with* Assetic, you can manipulate these assets however you want (or load them from anywhere) before serving them. This means you can: @@ -138,21 +132,11 @@ Including JavaScript Files To include JavaScript files, use the ``javascripts`` tag in any template: -.. configuration-block:: +.. code-block:: html+twig - .. code-block:: html+twig - - {% javascripts '@AppBundle/Resources/public/js/*' %} - - {% endjavascripts %} - - .. code-block:: html+php - - javascripts( - array('@AppBundle/Resources/public/js/*') - ) as $url): ?> - - + {% javascripts '@AppBundle/Resources/public/js/*' %} + + {% endjavascripts %} .. note:: @@ -194,22 +178,11 @@ Including CSS Stylesheets To bring in CSS stylesheets, you can use the same technique explained above, except with the ``stylesheets`` tag: -.. configuration-block:: +.. code-block:: html+twig - .. code-block:: html+twig - - {% stylesheets 'bundles/app/css/*' filter='cssrewrite' %} - - {% endstylesheets %} - - .. code-block:: html+php - - stylesheets( - array('bundles/app/css/*'), - array('cssrewrite') - ) as $url): ?> - - + {% stylesheets 'bundles/app/css/*' filter='cssrewrite' %} + + {% endstylesheets %} .. note:: @@ -247,21 +220,11 @@ Including Images To include an image you can use the ``image`` tag. -.. configuration-block:: - - .. code-block:: html+twig - - {% image '@AppBundle/Resources/public/images/example.jpg' %} - Example - {% endimage %} - - .. code-block:: html+php +.. code-block:: html+twig - image( - array('@AppBundle/Resources/public/images/example.jpg') - ) as $url): ?> - Example - + {% image '@AppBundle/Resources/public/images/example.jpg' %} + Example + {% endimage %} You can also use Assetic for image optimization. More information in :doc:`/frontend/assetic/jpeg_optimize`. @@ -299,28 +262,14 @@ manageable parts. This can help with re-usability as you can easily split project-specific files from those which can be used in other applications, but still serve them as a single file: -.. configuration-block:: - - .. code-block:: html+twig +.. code-block:: html+twig - {% javascripts - '@AppBundle/Resources/public/js/*' - '@AcmeBarBundle/Resources/public/js/form.js' - '@AcmeBarBundle/Resources/public/js/calendar.js' %} - - {% endjavascripts %} - - .. code-block:: html+php - - javascripts( - array( - '@AppBundle/Resources/public/js/*', - '@AcmeBarBundle/Resources/public/js/form.js', - '@AcmeBarBundle/Resources/public/js/calendar.js', - ) - ) as $url): ?> - - + {% javascripts + '@AppBundle/Resources/public/js/*' + '@AcmeBarBundle/Resources/public/js/form.js' + '@AcmeBarBundle/Resources/public/js/calendar.js' %} + + {% endjavascripts %} In the ``dev`` environment, each file is still served individually, so that you can debug problems more easily. However, in the ``prod`` environment @@ -338,26 +287,13 @@ the JavaScript files. And combining files doesn't only apply to *your* files. You can also use Assetic to combine third party assets, such as jQuery, with your own into a single file: -.. configuration-block:: +.. code-block:: html+twig - .. code-block:: html+twig - - {% javascripts - '@AppBundle/Resources/public/js/thirdparty/jquery.js' - '@AppBundle/Resources/public/js/*' %} - - {% endjavascripts %} - - .. code-block:: html+php - - javascripts( - array( - '@AppBundle/Resources/public/js/thirdparty/jquery.js', - '@AppBundle/Resources/public/js/*', - ) - ) as $url): ?> - - + {% javascripts + '@AppBundle/Resources/public/js/thirdparty/jquery.js' + '@AppBundle/Resources/public/js/*' %} + + {% endjavascripts %} Using Named Assets ~~~~~~~~~~~~~~~~~~ @@ -416,26 +352,13 @@ configuration under the ``assetic`` section. Read more in the After you have defined the named assets, you can reference them in your templates with the ``@named_asset`` notation: -.. configuration-block:: - - .. code-block:: html+twig - - {% javascripts - '@jquery_and_ui' - '@AppBundle/Resources/public/js/*' %} - - {% endjavascripts %} - - .. code-block:: html+php +.. code-block:: html+twig - javascripts( - array( - '@jquery_and_ui', - '@AppBundle/Resources/public/js/*', - ) - ) as $url): ?> - - + {% javascripts + '@jquery_and_ui' + '@AppBundle/Resources/public/js/*' %} + + {% endjavascripts %} .. _assetic-filters: @@ -506,22 +429,11 @@ should be defined: Now, to actually *use* the filter on a group of JavaScript files, add it into your template: -.. configuration-block:: - - .. code-block:: html+twig - - {% javascripts '@AppBundle/Resources/public/js/*' filter='uglifyjs2' %} - - {% endjavascripts %} - - .. code-block:: html+php +.. code-block:: html+twig - javascripts( - array('@AppBundle/Resources/public/js/*'), - array('uglifyjs2') - ) as $url): ?> - - + {% javascripts '@AppBundle/Resources/public/js/*' filter='uglifyjs2' %} + + {% endjavascripts %} A more detailed guide about configuring and using Assetic filters as well as details of Assetic's debug mode can be found in :doc:`/frontend/assetic/uglifyjs`. @@ -532,23 +444,11 @@ Controlling the URL Used If you wish to, you can control the URLs that Assetic produces. This is done from the template and is relative to the public document root: -.. configuration-block:: - - .. code-block:: html+twig - - {% javascripts '@AppBundle/Resources/public/js/*' output='js/compiled/main.js' %} - - {% endjavascripts %} +.. code-block:: html+twig - .. code-block:: html+php - - javascripts( - array('@AppBundle/Resources/public/js/*'), - array(), - array('output' => 'js/compiled/main.js') - ) as $url): ?> - - + {% javascripts '@AppBundle/Resources/public/js/*' output='js/compiled/main.js' %} + + {% endjavascripts %} .. note:: @@ -669,22 +569,10 @@ Since running this command in the ``dev`` environment may generate a bunch of files, it's usually a good idea to point your generated asset files to some isolated directory (e.g. ``/js/compiled``), to keep things organized: -.. configuration-block:: - - .. code-block:: html+twig - - {% javascripts '@AppBundle/Resources/public/js/*' output='js/compiled/main.js' %} - - {% endjavascripts %} - - .. code-block:: html+php +.. code-block:: html+twig - javascripts( - array('@AppBundle/Resources/public/js/*'), - array(), - array('output' => 'js/compiled/main.js') - ) as $url): ?> - - + {% javascripts '@AppBundle/Resources/public/js/*' output='js/compiled/main.js' %} + + {% endjavascripts %} .. _`LiipImagineBundle`: https://github.com/liip/LiipImagineBundle diff --git a/frontend/assetic/jpeg_optimize.rst b/frontend/assetic/jpeg_optimize.rst index 8e6871163d1..324975cbae7 100644 --- a/frontend/assetic/jpeg_optimize.rst +++ b/frontend/assetic/jpeg_optimize.rst @@ -61,23 +61,12 @@ using the ``bin`` option of the ``jpegoptim`` filter: It can now be used from a template: -.. configuration-block:: - - .. code-block:: html+twig - - {% image '@AppBundle/Resources/public/images/example.jpg' - filter='jpegoptim' output='/images/example.jpg' %} - Example - {% endimage %} - - .. code-block:: html+php +.. code-block:: html+twig - image( - array('@AppBundle/Resources/public/images/example.jpg'), - array('jpegoptim') - ) as $url): ?> - Example - + {% image '@AppBundle/Resources/public/images/example.jpg' + filter='jpegoptim' output='/images/example.jpg' %} + Example + {% endimage %} Removing all EXIF Data ~~~~~~~~~~~~~~~~~~~~~~ diff --git a/frontend/assetic/uglifyjs.rst b/frontend/assetic/uglifyjs.rst index 89b39fd7b4a..4da6c61e005 100644 --- a/frontend/assetic/uglifyjs.rst +++ b/frontend/assetic/uglifyjs.rst @@ -183,22 +183,11 @@ Minify your Assets In order to apply UglifyJS on your assets, add the ``filter`` option in the asset tags of your templates to tell Assetic to use the ``uglifyjs2`` filter: -.. configuration-block:: - - .. code-block:: html+twig - - {% javascripts '@AppBundle/Resources/public/js/*' filter='uglifyjs2' %} - - {% endjavascripts %} +.. code-block:: html+twig - .. code-block:: html+php - - javascripts( - array('@AppBundle/Resources/public/js/*'), - array('uglifyj2s') - ) as $url): ?> - - + {% javascripts '@AppBundle/Resources/public/js/*' filter='uglifyjs2' %} + + {% endjavascripts %} .. note:: @@ -218,22 +207,11 @@ debug (e.g. ``app_dev.php``) mode. You can do this by prefixing the filter name in your template with a question mark: ``?``. This tells Assetic to only apply this filter when debug mode is off (e.g. ``app.php``): -.. configuration-block:: - - .. code-block:: html+twig +.. code-block:: html+twig - {% javascripts '@AppBundle/Resources/public/js/*' filter='?uglifyjs2' %} - - {% endjavascripts %} - - .. code-block:: html+php - - javascripts( - array('@AppBundle/Resources/public/js/*'), - array('?uglifyjs2') - ) as $url): ?> - - + {% javascripts '@AppBundle/Resources/public/js/*' filter='?uglifyjs2' %} + + {% endjavascripts %} To try this out, switch to your ``prod`` environment (``app.php``). But before you do, don't forget to :ref:`clear your cache ` @@ -305,23 +283,11 @@ Next, add the configuration for this filter: To use the filter for your CSS files, add the filter to the Assetic ``stylesheets`` helper: -.. configuration-block:: - - .. code-block:: html+twig - - {% stylesheets 'bundles/App/css/*' filter='uglifycss' filter='cssrewrite' %} - - {% endstylesheets %} - - .. code-block:: html+php +.. code-block:: html+twig - stylesheets( - array('bundles/App/css/*'), - array('uglifycss'), - array('cssrewrite') - ) as $url): ?> - - + {% stylesheets 'bundles/App/css/*' filter='uglifycss' filter='cssrewrite' %} + + {% endstylesheets %} Just like with the ``uglifyjs2`` filter, if you prefix the filter name with ``?`` (i.e. ``?uglifycss``), the minification will only happen when you're diff --git a/frontend/assetic/yuicompressor.rst b/frontend/assetic/yuicompressor.rst index 1b71bb417c3..c0d4aa8018a 100644 --- a/frontend/assetic/yuicompressor.rst +++ b/frontend/assetic/yuicompressor.rst @@ -95,22 +95,11 @@ You have YUI Compressor configured now, but nothing is going to happen until you apply one of these filters to an asset. Since your assets are a part of the view layer, this work is done in your templates: -.. configuration-block:: - - .. code-block:: html+twig - - {% javascripts '@AppBundle/Resources/public/js/*' filter='yui_js' %} - - {% endjavascripts %} +.. code-block:: html+twig - .. code-block:: html+php - - javascripts( - array('@AppBundle/Resources/public/js/*'), - array('yui_js') - ) as $url): ?> - - + {% javascripts '@AppBundle/Resources/public/js/*' filter='yui_js' %} + + {% endjavascripts %} .. note:: @@ -123,22 +112,11 @@ With the addition of the ``yui_js`` filter to the asset tags above, you should now see minified JavaScripts coming over the wire much faster. The same process can be repeated to minify your stylesheets. -.. configuration-block:: - - .. code-block:: html+twig +.. code-block:: html+twig - {% stylesheets '@AppBundle/Resources/public/css/*' filter='yui_css' %} - - {% endstylesheets %} - - .. code-block:: html+php - - stylesheets( - array('@AppBundle/Resources/public/css/*'), - array('yui_css') - ) as $url): ?> - - + {% stylesheets '@AppBundle/Resources/public/css/*' filter='yui_css' %} + + {% endstylesheets %} Disable Minification in Debug Mode ---------------------------------- @@ -149,22 +127,11 @@ application is in debug mode. You can do this by prefixing the filter name in your template with a question mark: ``?``. This tells Assetic to only apply this filter when debug mode is off. -.. configuration-block:: - - .. code-block:: html+twig - - {% javascripts '@AppBundle/Resources/public/js/*' filter='?yui_js' %} - - {% endjavascripts %} - - .. code-block:: html+php +.. code-block:: html+twig - javascripts( - array('@AppBundle/Resources/public/js/*'), - array('?yui_js') - ) as $url): ?> - - + {% javascripts '@AppBundle/Resources/public/js/*' filter='?yui_js' %} + + {% endjavascripts %} .. tip:: diff --git a/reference/configuration/framework.rst b/reference/configuration/framework.rst index fee66841949..adb53871f19 100644 --- a/reference/configuration/framework.rst +++ b/reference/configuration/framework.rst @@ -1153,15 +1153,9 @@ You can group assets into packages, to specify different base URLs for them: Now you can use the ``avatars`` package in your templates: -.. configuration-block:: php +.. code-block:: html+twig - .. code-block:: html+twig - - - - .. code-block:: html+php - - + Each package can configure the following options: @@ -1185,15 +1179,9 @@ equivalent) as well as assets rendered with Assetic. For example, suppose you have the following: -.. configuration-block:: - - .. code-block:: html+twig - - Symfony! - - .. code-block:: php +.. code-block:: html+twig - Symfony! + Symfony! By default, this will render a path to your image such as ``/images/logo.png``. Now, activate the ``version`` option: diff --git a/reference/forms/twig_reference.rst b/reference/forms/twig_reference.rst index 581061f75ae..96c19ab9196 100644 --- a/reference/forms/twig_reference.rst +++ b/reference/forms/twig_reference.rst @@ -315,21 +315,12 @@ reference the variables on the ``name`` field, accessing the variables is done by using a public ``vars`` property on the :class:`Symfony\\Component\\Form\\FormView` object: -.. configuration-block:: - - .. code-block:: html+twig - - - - .. code-block:: html+php +.. code-block:: html+twig - + .. versionadded:: 2.3 The ``method`` and ``action`` variables were introduced in Symfony 2.3. diff --git a/reference/forms/types/collection.rst b/reference/forms/types/collection.rst index faa1c5fcefb..17f9b03d5dd 100644 --- a/reference/forms/types/collection.rst +++ b/reference/forms/types/collection.rst @@ -69,47 +69,25 @@ address as its own input text box:: The simplest way to render this is all at once: -.. configuration-block:: +.. code-block:: twig - .. code-block:: twig - - {{ form_row(form.emails) }} - - .. code-block:: php - - row($form['emails']) ?> + {{ form_row(form.emails) }} A much more flexible method would look like this: -.. configuration-block:: - - .. code-block:: html+twig - - {{ form_label(form.emails) }} - {{ form_errors(form.emails) }} - - - - .. code-block:: html+php +.. code-block:: html+twig - label($form['emails']) ?> - errors($form['emails']) ?> + {{ form_label(form.emails) }} + {{ form_errors(form.emails) }} - + In both cases, no input fields would render unless your ``emails`` data array already contained some emails. @@ -359,15 +337,9 @@ be added to your underlying array due to the `allow_add`_ option. The prototype field can be rendered via the ``prototype`` variable in the collection field: -.. configuration-block:: - - .. code-block:: twig - - {{ form_row(form.emails.vars.prototype) }} - - .. code-block:: php +.. code-block:: twig - row($form['emails']->vars['prototype']) ?> + {{ form_row(form.emails.vars.prototype) }} Note that all you really need is the "widget", but depending on how you're rendering your form, having the entire "form row" may be easier for you. diff --git a/reference/forms/types/repeated.rst b/reference/forms/types/repeated.rst index 3a100827ee6..17e2c52e994 100644 --- a/reference/forms/types/repeated.rst +++ b/reference/forms/types/repeated.rst @@ -68,30 +68,17 @@ The repeated field type is actually two underlying fields, which you can render all at once, or individually. To render all at once, use something like: -.. configuration-block:: +.. code-block:: twig - .. code-block:: twig - - {{ form_row(form.password) }} - - .. code-block:: php - - row($form['password']) ?> + {{ form_row(form.password) }} To render each field individually, use something like this: -.. configuration-block:: - - .. code-block:: twig - - {# .first and .second may vary in your use - see the note below #} - {{ form_row(form.password.first) }} - {{ form_row(form.password.second) }} - - .. code-block:: php +.. code-block:: twig - row($form['password']['first']) ?> - row($form['password']['second']) ?> + {# .first and .second may vary in your use - see the note below #} + {{ form_row(form.password.first) }} + {{ form_row(form.password.second) }} .. note:: diff --git a/security.rst b/security.rst index 0ba07f778ad..62def3292cb 100644 --- a/security.rst +++ b/security.rst @@ -879,19 +879,11 @@ Access Control in Templates If you want to check if the current user has a role inside a template, use the built-in ``is_granted()`` helper function: -.. configuration-block:: - - .. code-block:: html+twig +.. code-block:: html+twig - {% if is_granted('ROLE_ADMIN') %} - Delete - {% endif %} - - .. code-block:: html+php - - isGranted('ROLE_ADMIN')): ?> - Delete - + {% if is_granted('ROLE_ADMIN') %} + Delete + {% endif %} .. note:: @@ -1070,19 +1062,11 @@ Retrieving the User in a Template In a Twig Template this object can be accessed via the :ref:`app.user ` key: -.. configuration-block:: - - .. code-block:: html+twig - - {% if is_granted('IS_AUTHENTICATED_FULLY') %} -

Username: {{ app.user.username }}

- {% endif %} - - .. code-block:: html+php +.. code-block:: html+twig - isGranted('IS_AUTHENTICATED_FULLY')): ?> -

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

- + {% if is_granted('IS_AUTHENTICATED_FULLY') %} +

Username: {{ app.user.username }}

+ {% endif %} .. _security-logging-out: diff --git a/templating.rst b/templating.rst index 33427cf8c2f..413e3ae4807 100644 --- a/templating.rst +++ b/templating.rst @@ -170,59 +170,30 @@ that overrides certain methods of its parent class"). First, build a base layout file: -.. configuration-block:: +.. code-block:: html+twig - .. code-block:: html+twig + {# app/Resources/views/base.html.twig #} + + + + + {% block title %}Test Application{% endblock %} + + + - {# app/Resources/views/base.html.twig #} - - - - - {% block title %}Test Application{% endblock %} - - - - -
- {% block body %}{% endblock %} -
- - - - .. code-block:: html+php - - - - - - - <?php $view['slots']->output('title', 'Test Application') ?> - - - - -
- output('body') ?> -
- - +
+ {% block body %}{% endblock %} +
+ + .. note:: @@ -238,35 +209,19 @@ simply retain the default values used in this template. A child template might look like this: -.. configuration-block:: - - .. code-block:: html+twig - - {# app/Resources/views/blog/index.html.twig #} - {% extends 'base.html.twig' %} - - {% block title %}My cool blog posts{% endblock %} - - {% block body %} - {% for entry in blog_entries %} -

{{ entry.title }}

-

{{ entry.body }}

- {% endfor %} - {% endblock %} - - .. code-block:: html+php +.. code-block:: html+twig - - extend('base.html.php') ?> + {# app/Resources/views/blog/index.html.twig #} + {% extends 'base.html.twig' %} - set('title', 'My cool blog posts') ?> + {% block title %}My cool blog posts{% endblock %} - start('body') ?> - -

getTitle() ?>

-

getBody() ?>

- - stop() ?> + {% block body %} + {% for entry in blog_entries %} +

{{ entry.title }}

+

{{ entry.body }}

+ {% endfor %} + {% endblock %} .. note:: @@ -480,60 +435,30 @@ a new PHP class or function. The same is true for templates. By moving the reused template code into its own template, it can be included from any other template. First, create the template that you'll need to reuse. -.. configuration-block:: - - .. code-block:: html+twig - - {# app/Resources/views/article/article_details.html.twig #} -

{{ article.title }}

- - -

- {{ article.body }} -

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

getTitle() ?>

- + {# app/Resources/views/article/article_details.html.twig #} +

{{ article.title }}

+ -

- getBody() ?> -

+

+ {{ article.body }} +

Including this template from any other template is simple: -.. configuration-block:: - - .. code-block:: html+twig - - {# app/Resources/views/article/list.html.twig #} - {% extends 'layout.html.twig' %} - - {% block body %} -

Recent Articles

- - {% for article in articles %} - {{ include('article/article_details.html.twig', { 'article': article }) }} - {% endfor %} - {% endblock %} - - .. code-block:: html+php +.. code-block:: html+twig - - extend('layout.html.php') ?> + {# app/Resources/views/article/list.html.twig #} + {% extends 'layout.html.twig' %} - start('body') ?> -

Recent Articles

+ {% block body %} +

Recent Articles

- - render( - 'Article/article_details.html.php', - array('article' => $article) - ) ?> - - stop() ?> + {% for article in articles %} + {{ include('article/article_details.html.twig', { 'article': article }) }} + {% endfor %} + {% endblock %} The template is included using the ``{{ include() }}`` function. Notice that the template name follows the same typical convention. The ``article_details.html.twig`` @@ -626,15 +551,9 @@ configuration: To link to the page, just use the ``path()`` Twig function and refer to the route: -.. configuration-block:: - - .. code-block:: html+twig - - Home - - .. code-block:: html+php +.. code-block:: html+twig - Home + Home .. versionadded:: 2.8 The ``path()`` PHP templating helper was introduced in Symfony 2.8. Prior @@ -702,44 +621,22 @@ a value for the ``{slug}`` parameter. Using this route, revisit the ``recent_list.html.twig`` template from the previous section and link to the articles correctly: -.. configuration-block:: - - .. code-block:: html+twig - - {# app/Resources/views/article/recent_list.html.twig #} - {% for article in articles %} - - {{ article.title }} - - {% endfor %} - - .. code-block:: html+php +.. code-block:: html+twig - - - - getTitle() ?> - - + {# app/Resources/views/article/recent_list.html.twig #} + {% for article in articles %} + + {{ article.title }} + + {% endfor %} .. tip:: You can also generate an absolute URL by using the ``url()`` Twig function: - .. configuration-block:: - - .. code-block:: html+twig - - Home - - .. code-block:: html+php +.. code-block:: html+twig - Home + Home .. versionadded:: 2.8 The ``url()`` PHP templating helper was introduced in Symfony 2.8. Prior @@ -759,19 +656,11 @@ Templates also commonly refer to images, JavaScript, stylesheets and other assets. Of course you could hard-code the path to these assets (e.g. ``/images/logo.png``), but Symfony provides a more dynamic option via the ``asset()`` Twig function: -.. configuration-block:: - - .. code-block:: html+twig - - Symfony! - - - - .. code-block:: html+php +.. code-block:: html+twig - Symfony! + Symfony! - + The ``asset()`` function's main purpose is to make your application more portable. If your application lives at the root of your host (e.g. ``http://example.com``), @@ -819,76 +708,43 @@ one called ``stylesheets`` inside the ``head`` tag and another called ``javascri just above the closing ``body`` tag. These blocks will contain all of the stylesheets and JavaScripts that you'll need throughout your site: -.. configuration-block:: - - .. code-block:: html+twig - - {# app/Resources/views/base.html.twig #} - - - {# ... #} - - {% block stylesheets %} - - {% endblock %} - - - {# ... #} - - {% block javascripts %} - - {% endblock %} - - - - .. code-block:: php +.. code-block:: html+twig - // app/Resources/views/base.html.php - - - + {# app/Resources/views/base.html.twig #} + + + {# ... #} - start('stylesheets') ?> - - stop() ?> - - - + {% block stylesheets %} + + {% endblock %} + + + {# ... #} - start('javascripts') ?> - - stop() ?> - - + {% block javascripts %} + + {% endblock %} + + That's easy enough! But what if you need to include an extra stylesheet or JavaScript from a child template? For example, suppose you have a contact page and you need to include a ``contact.css`` stylesheet *just* on that page. From inside that contact page's template, do the following: -.. configuration-block:: - - .. code-block:: html+twig - - {# app/Resources/views/contact/contact.html.twig #} - {% extends 'base.html.twig' %} - - {% block stylesheets %} - {{ parent() }} - - - {% endblock %} +.. code-block:: html+twig - {# ... #} + {# app/Resources/views/contact/contact.html.twig #} + {% extends 'base.html.twig' %} - .. code-block:: php + {% block stylesheets %} + {{ parent() }} - // app/Resources/views/contact/contact.html.twig - extend('base.html.php') ?> + + {% endblock %} - start('stylesheets') ?> - - stop() ?> + {# ... #} In the child template, you simply override the ``stylesheets`` block and put your new stylesheet tag inside of that block. Of course, since you want diff --git a/validation.rst b/validation.rst index 2196107c11b..cdd2be841fc 100644 --- a/validation.rst +++ b/validation.rst @@ -179,27 +179,15 @@ You could also pass the collection of errors into a template:: Inside the template, you can output the list of errors exactly as needed: -.. configuration-block:: - - .. code-block:: html+twig - - {# app/Resources/views/author/validation.html.twig #} -

The author has the following errors

-
    - {% for error in errors %} -
  • {{ error.message }}
  • - {% endfor %} -
- - .. code-block:: html+php - - -

The author has the following errors

-
    - -
  • getMessage() ?>
  • - -
+.. code-block:: html+twig + + {# app/Resources/views/author/validation.html.twig #} +

The author has the following errors

+
    + {% for error in errors %} +
  • {{ error.message }}
  • + {% endfor %} +
.. note::