Skip to content

Commit 0457280

Browse files
committed
feature symfony#5574 [2.7] Update Twig docs for asset features (javiereguiluz, WouterJ)
This PR was merged into the 2.7 branch. Discussion ---------- [2.7] Update Twig docs for asset features Finishes symfony#5171 | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | 2.7+ | Fixed tickets | symfony#4982 (partially) Commits ------- 0cd7e6c Correctly document new twig functions bc18ff1 Updated Twig template to take into account asset() function changes
2 parents c775a6f + 0cd7e6c commit 0457280

File tree

3 files changed

+29
-54
lines changed

3 files changed

+29
-54
lines changed

book/templating.rst

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,46 +1067,12 @@ assets won't be loaded from cache after being deployed. For example, ``/images/l
10671067
look like ``/images/logo.png?v2``. For more information, see the :ref:`reference-framework-assets-version`
10681068
configuration option.
10691069

1070-
.. _`book-templating-version-by-asset`:
1070+
If you need absolute URLs for assets, use the ``absolute_url()`` Twig function
1071+
as follows:
10711072

1072-
If you need to set a version for a specific asset, you can set the ``version`` argument
1073-
if you are using Twig (or the fourth argument if you are using PHP) to the desired version:
1073+
.. code-block:: html+jinja
10741074

1075-
.. configuration-block::
1076-
1077-
.. code-block:: html+jinja
1078-
1079-
<img src="{{ asset('images/logo.png', version='3.0') }}" alt="Symfony!" />
1080-
1081-
.. code-block:: html+php
1082-
1083-
<img src="<?php echo $view['assets']->getUrl(
1084-
'images/logo.png',
1085-
null,
1086-
false,
1087-
'3.0'
1088-
) ?>" alt="Symfony!" />
1089-
1090-
If you don't give a version or pass ``null``, the default package version
1091-
(from :ref:`reference-framework-assets-version`) will be used. If you pass ``false``,
1092-
versioned URL will be deactivated for this asset.
1093-
1094-
If you need absolute URLs for assets, you can use the ``absolute_url`` function
1095-
if you are using Twig (or the third argument if you are using PHP) to ``true``:
1096-
1097-
.. configuration-block::
1098-
1099-
.. code-block:: html+jinja
1100-
1101-
<img src="{{ absolute_url(asset('images/logo.png')) }}" alt="Symfony!" />
1102-
1103-
.. code-block:: html+php
1104-
1105-
<img src="<?php echo $view['assets']->getUrl(
1106-
'images/logo.png',
1107-
null,
1108-
true
1109-
) ?>" alt="Symfony!" />
1075+
<img src="{{ absolute_url(asset('images/logo.png')) }}" alt="Symfony!" />
11101076

11111077
.. index::
11121078
single: Templating; Including stylesheets and JavaScripts

reference/configuration/framework.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,10 +1081,6 @@ Now, the same asset will be rendered as ``/images/logo.png?v2`` If you use
10811081
this feature, you **must** manually increment the ``version`` value
10821082
before each deployment so that the query parameters change.
10831083

1084-
It's also possible to set the version value on an asset-by-asset basis (instead
1085-
of using the global version - e.g. ``v2`` - set here). See
1086-
:ref:`Versioning by Asset <book-templating-version-by-asset>` for details.
1087-
10881084
You can also control how the query string works via the `version_format`_
10891085
option.
10901086

reference/twig_reference.rst

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ asset
111111

112112
Returns a public path to ``path``, which takes into account the base path
113113
set for the package and the URL path. More information in
114-
:ref:`book-templating-assets`. For asset versioning, see :ref:`reference-framework-assets-version`.
114+
:ref:`book-templating-assets`. For asset versioning, see
115+
:ref:`reference-framework-assets-version`.
115116

116117
assets_version
117118
~~~~~~~~~~~~~~
@@ -362,8 +363,8 @@ information in :ref:`book-templating-pages`.
362363
absolute_url
363364
~~~~~~~~~~~~
364365

365-
.. versionadded:: 2.6
366-
The ``absolute_url`` function was introduced in Symfony 2.7
366+
.. versionadded:: 2.7
367+
The ``absolute_url()`` function was introduced in Symfony 2.7.
367368

368369
.. code-block:: jinja
369370
@@ -372,18 +373,23 @@ absolute_url
372373
``path``
373374
**type**: ``string``
374375

375-
Returns the absolute URL for the given absolute path. This is useful to convert
376-
an existing path:
376+
Returns the absolute URL from the passed relative path. For example, assume
377+
you're on the following page in your app:
378+
``http://example.com/products/hover-board``.
377379

378380
.. code-block:: jinja
379381
380-
{{ absolute_url(asset(path)) }}
382+
{{ absolute_url('/human.txt') }}
383+
{# http://example.com/human.txt #}
384+
385+
{{ absolute_url('products_icon.png') }}
386+
{# http://example.com/products/products_icon.png #}
381387
382388
relative_path
383389
~~~~~~~~~~~~~
384390

385-
.. versionadded:: 2.6
386-
The ``relative_path`` function was introduced in Symfony 2.7
391+
.. versionadded:: 2.7
392+
The ``relative_path()`` function was introduced in Symfony 2.7.
387393

388394
.. code-block:: jinja
389395
@@ -392,10 +398,17 @@ relative_path
392398
``path``
393399
**type**: ``string``
394400

395-
Returns a relative path for the given absolute path (based on the current
396-
request path). For instance, if the current path is
397-
``/article/news/welcome.html``, the relative path for ``/article/image.png`` is
398-
``../images.png``.
401+
Returns the relative path from the passed absolute URL. For example, assume
402+
you're on the following page in your app:
403+
``http://example.com/products/hover-board``.
404+
405+
.. code-block:: jinja
406+
407+
{{ relative_path('http://example.com/human.txt') }}
408+
{# ../human.txt #}
409+
410+
{{ relative_path('http://example.com/products/products_icon.png') }}
411+
{# products_icon.png #}
399412
400413
expression
401414
~~~~~~~~~~

0 commit comments

Comments
 (0)