Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bd947c7

Browse files
committedMar 17, 2014
[TwigBundle] Add documentation about generating absolute URL with the asset function
1 parent 739f43f commit bd947c7

File tree

3 files changed

+104
-71
lines changed

3 files changed

+104
-71
lines changed
 

‎book/templating.rst‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,22 @@ assets won't be cached when deployed. For example, ``/images/logo.png`` might
991991
look like ``/images/logo.png?v2``. For more information, see the :ref:`ref-framework-assets-version`
992992
configuration option.
993993

994+
.. versionadded:: 2.5
995+
Absolute URLs for assets were introduced in Symfony 2.5.
996+
997+
If you need absolute URLs for assets, you can set the third argument (or the
998+
``absolute`` argument) to ``true``:
999+
1000+
.. configuration-block::
1001+
1002+
.. code-block:: html+jinja
1003+
1004+
<img src="{{ asset('images/logo.png', absolute=true) }}" alt="Symfony!" />
1005+
1006+
.. code-block:: html+php
1007+
1008+
<img src="<?php echo $view['assets']->getUrl('images/logo.png', null, true) ?>" alt="Symfony!" />
1009+
9941010
.. index::
9951011
single: Templating; Including stylesheets and JavaScripts
9961012
single: Stylesheets; Including stylesheets

‎components/templating/helpers/assetshelper.rst‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ You can also specify a URL to use in the second parameter of the constructor::
4747

4848
Now URLs are rendered like ``http://cdn.example.com/images/logo.png``.
4949

50+
.. versionadded:: 2.5
51+
Absolute URLs for assets were introduced in Symfony 2.5.
52+
53+
You can also use the third argument of the helper to force an absolute URL:
54+
55+
.. code-block:: html+php
56+
57+
<img src="<?php echo $view['assets']->getUrl('images/logo.png', null, true) ?>">
58+
<!-- renders as:
59+
<img src="http://yourwebsite.com/foo/bar/images/logo.png">
60+
-->
61+
62+
.. note::
63+
64+
If you already set a URL in the constructor, using the third argument of
65+
``getUrl`` will not affect the generated URL.
66+
5067
Versioning
5168
----------
5269

‎reference/twig_reference.rst‎

Lines changed: 71 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -20,77 +20,77 @@ Functions
2020
.. versionadded:: 2.4
2121
The ``expression`` function was introduced in Symfony 2.4.
2222

23-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
24-
| Function Syntax | Usage |
25-
+====================================================+============================================================================================+
26-
| ``render(uri, options = {})`` | This will render the fragment for the given controller or URL |
27-
| ``render(controller('B:C:a', {params}))`` | For more information, see :ref:`templating-embedding-controller`. |
28-
| ``render(path('route', {params}))`` | |
29-
| ``render(url('route', {params}))`` | |
30-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
31-
| ``render_esi(controller('B:C:a', {params}))`` | This will generate an ESI tag when possible or fallback to the ``render`` |
32-
| ``render_esi(url('route', {params}))`` | behavior otherwise. For more information, see :ref:`templating-embedding-controller`. |
33-
| ``render_esi(path('route', {params}))`` | |
34-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
35-
| ``render_hinclude(controller(...))`` | This will generates an Hinclude tag for the given controller or URL. |
36-
| ``render_hinclude(url('route', {params}))`` | For more information, see :ref:`templating-embedding-controller`. |
37-
| ``render_hinclude(path('route', {params}))`` | |
38-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
39-
| ``controller(attributes = {}, query = {})`` | Used along with the ``render`` tag to refer to the controller that you want to render. |
40-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
41-
| ``asset(path, packageName = null)`` | Get the public path of the asset, more information in |
42-
| | ":ref:`book-templating-assets`". |
43-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
44-
| ``asset_version(packageName = null)`` | Get the current version of the package, more information in |
45-
| | ":ref:`book-templating-assets`". |
46-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
47-
| ``form(view, variables = {})`` | This will render the HTML of a complete form, more information in |
48-
| | in :ref:`the Twig Form reference<reference-forms-twig-form>`. |
49-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
50-
| ``form_start(view, variables = {})`` | This will render the HTML start tag of a form, more information in |
51-
| | in :ref:`the Twig Form reference<reference-forms-twig-start>`. |
52-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
53-
| ``form_end(view, variables = {})`` | This will render the HTML end tag of a form together with all fields that |
54-
| | have not been rendered yet, more information |
55-
| | in :ref:`the Twig Form reference<reference-forms-twig-end>`. |
56-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
57-
| ``form_enctype(view)`` | This will render the required ``enctype="multipart/form-data"`` attribute |
58-
| | if the form contains at least one file upload field, more information in |
59-
| | in :ref:`the Twig Form reference <reference-forms-twig-enctype>`. |
60-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
61-
| ``form_widget(view, variables = {})`` | This will render a complete form or a specific HTML widget of a field, |
62-
| | more information in :ref:`the Twig Form reference <reference-forms-twig-widget>`. |
63-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
64-
| ``form_errors(view)`` | This will render any errors for the given field or the "global" errors, |
65-
| | more information in :ref:`the Twig Form reference <reference-forms-twig-errors>`. |
66-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
67-
| ``form_label(view, label = null, variables = {})`` | This will render the label for the given field, more information in |
68-
| | :ref:`the Twig Form reference <reference-forms-twig-label>`. |
69-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
70-
| ``form_row(view, variables = {})`` | This will render the row (the field's label, errors and widget) of the given |
71-
| | field, more information in :ref:`the Twig Form reference <reference-forms-twig-row>`. |
72-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
73-
| ``form_rest(view, variables = {})`` | This will render all fields that have not yet been rendered, more |
74-
| | information in :ref:`the Twig Form reference <reference-forms-twig-rest>`. |
75-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
76-
| ``csrf_token(intention)`` | This will render a CSRF token. Use this function if you want CSRF protection without |
77-
| | creating a form |
78-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
79-
| ``is_granted(role, object = null, field = null)`` | This will return ``true`` if the current user has the required role, more |
80-
| | information in ":ref:`book-security-template`" |
81-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
82-
| ``logout_path(key)`` | This will generate the relative logout URL for the given firewall |
83-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
84-
| ``logout_url(key)`` | Equal to ``logout_path(...)`` but this will generate an absolute URL |
85-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
86-
| ``path(name, parameters = {})`` | Get a relative URL for the given route, more information in |
87-
| | ":ref:`book-templating-pages`". |
88-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
89-
| ``url(name, parameters = {})`` | Equal to ``path(...)`` but it generates an absolute URL |
90-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
91-
| ``expression(expression)`` | Creates an :class:`Symfony\\Component\\ExpressionLanguage\\Expression` in Twig. See |
92-
| | ":ref:`Template Expressions <book-security-template-expression>`". |
93-
+----------------------------------------------------+--------------------------------------------------------------------------------------------+
23+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
24+
| Function Syntax | Usage |
25+
+=======================================================+============================================================================================+
26+
| ``render(uri, options = {})`` | This will render the fragment for the given controller or URL |
27+
| ``render(controller('B:C:a', {params}))`` | For more information, see :ref:`templating-embedding-controller`. |
28+
| ``render(path('route', {params}))`` | |
29+
| ``render(url('route', {params}))`` | |
30+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
31+
| ``render_esi(controller('B:C:a', {params}))`` | This will generate an ESI tag when possible or fallback to the ``render`` |
32+
| ``render_esi(url('route', {params}))`` | behavior otherwise. For more information, see :ref:`templating-embedding-controller`. |
33+
| ``render_esi(path('route', {params}))`` | |
34+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
35+
| ``render_hinclude(controller(...))`` | This will generates an Hinclude tag for the given controller or URL. |
36+
| ``render_hinclude(url('route', {params}))`` | For more information, see :ref:`templating-embedding-controller`. |
37+
| ``render_hinclude(path('route', {params}))`` | |
38+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
39+
| ``controller(attributes = {}, query = {})`` | Used along with the ``render`` tag to refer to the controller that you want to render. |
40+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
41+
| ``asset(path, packageName = null, absolute = false)`` | Get the public path of the asset, more information in |
42+
| | ":ref:`book-templating-assets`". |
43+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
44+
| ``asset_version(packageName = null)`` | Get the current version of the package, more information in |
45+
| | ":ref:`book-templating-assets`". |
46+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
47+
| ``form(view, variables = {})`` | This will render the HTML of a complete form, more information in |
48+
| | in :ref:`the Twig Form reference<reference-forms-twig-form>`. |
49+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
50+
| ``form_start(view, variables = {})`` | This will render the HTML start tag of a form, more information in |
51+
| | in :ref:`the Twig Form reference<reference-forms-twig-start>`. |
52+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
53+
| ``form_end(view, variables = {})`` | This will render the HTML end tag of a form together with all fields that |
54+
| | have not been rendered yet, more information |
55+
| | in :ref:`the Twig Form reference<reference-forms-twig-end>`. |
56+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
57+
| ``form_enctype(view)`` | This will render the required ``enctype="multipart/form-data"`` attribute |
58+
| | if the form contains at least one file upload field, more information in |
59+
| | in :ref:`the Twig Form reference <reference-forms-twig-enctype>`. |
60+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
61+
| ``form_widget(view, variables = {})`` | This will render a complete form or a specific HTML widget of a field, |
62+
| | more information in :ref:`the Twig Form reference <reference-forms-twig-widget>`. |
63+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
64+
| ``form_errors(view)`` | This will render any errors for the given field or the "global" errors, |
65+
| | more information in :ref:`the Twig Form reference <reference-forms-twig-errors>`. |
66+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
67+
| ``form_label(view, label = null, variables = {})`` | This will render the label for the given field, more information in |
68+
| | :ref:`the Twig Form reference <reference-forms-twig-label>`. |
69+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
70+
| ``form_row(view, variables = {})`` | This will render the row (the field's label, errors and widget) of the given |
71+
| | field, more information in :ref:`the Twig Form reference <reference-forms-twig-row>`. |
72+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
73+
| ``form_rest(view, variables = {})`` | This will render all fields that have not yet been rendered, more |
74+
| | information in :ref:`the Twig Form reference <reference-forms-twig-rest>`. |
75+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
76+
| ``csrf_token(intention)`` | This will render a CSRF token. Use this function if you want CSRF protection without |
77+
| | creating a form |
78+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
79+
| ``is_granted(role, object = null, field = null)`` | This will return ``true`` if the current user has the required role, more |
80+
| | information in ":ref:`book-security-template`" |
81+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
82+
| ``logout_path(key)`` | This will generate the relative logout URL for the given firewall |
83+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
84+
| ``logout_url(key)`` | Equal to ``logout_path(...)`` but this will generate an absolute URL |
85+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
86+
| ``path(name, parameters = {})`` | Get a relative URL for the given route, more information in |
87+
| | ":ref:`book-templating-pages`". |
88+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
89+
| ``url(name, parameters = {})`` | Equal to ``path(...)`` but it generates an absolute URL |
90+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
91+
| ``expression(expression)`` | Creates an :class:`Symfony\\Component\\ExpressionLanguage\\Expression` in Twig. See |
92+
| | ":ref:`Template Expressions <book-security-template-expression>`". |
93+
+-------------------------------------------------------+--------------------------------------------------------------------------------------------+
9494

9595
Filters
9696
-------

0 commit comments

Comments
 (0)
Please sign in to comment.