Skip to content

Updated Twig template to take into account asset() function changes #5171

New issue

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

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

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 6 additions & 40 deletions book/templating.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Throughout this chapter, template examples will be shown in both Twig and PHP.
web designers everywhere.

Twig can also do things that PHP can't, such as whitespace control,
sandboxing, automatic HTML escaping, manual contextual output escaping,
sandboxing, automatic HTML escaping, manual contextual output escaping,
and the inclusion of custom functions and filters that only affect templates.
Twig contains little features that make writing templates easier and more concise.
Take the following example, which combines a loop with a logical ``if``
Expand Down Expand Up @@ -1021,46 +1021,12 @@ assets won't be cached when deployed. For example, ``/images/logo.png`` might
look like ``/images/logo.png?v2``. For more information, see the :ref:`ref-framework-assets-version`
configuration option.

.. _`book-templating-version-by-asset`:
If you need absolute URLs for assets, use the ``absolute_url()`` Twig function
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we have anything version-related left in the docs when this is removed?

as follows:

If you need to set a version for a specific asset, you can set the fourth
argument (or the ``version`` argument) to the desired version:

.. configuration-block::

.. code-block:: html+jinja

<img src="{{ asset('images/logo.png', version='3.0') }}" alt="Symfony!" />

.. code-block:: html+php

<img src="<?php echo $view['assets']->getUrl(
'images/logo.png',
null,
false,
'3.0'
) ?>" alt="Symfony!" />

If you don't give a version or pass ``null``, the default package version
(from :ref:`ref-framework-assets-version`) will be used. If you pass ``false``,
versioned URL will be deactivated for this asset.

If you need absolute URLs for assets, you can set the third argument (or the
``absolute`` argument) to ``true``:

.. configuration-block::

.. code-block:: html+jinja

<img src="{{ asset('images/logo.png', absolute=true) }}" alt="Symfony!" />

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

<img src="<?php echo $view['assets']->getUrl(
'images/logo.png',
null,
true
) ?>" alt="Symfony!" />
<img src="{{ absolute_url(asset('images/logo.png')) }}" alt="Symfony!" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have to document the PHP format

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the PHP format:

<img src="<?php echo $view['assets']->getUrl('images/logo.png') ?>" alt="Symfony!" />

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm no sorry. I'm missing the absolute_url part. I'm not sure we have the helper for that in the PHP templating engine though

however, the exsting doc is wrong too. Even in the deprecated asset system, there is no way to get an absolute URL in the PHP helper (the deprecated third argument is the version, not an absolute flag)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no PHP helper for it, it should be added during this stabilisation phase imo. Symfony 2.x still has to support PHP templating for 100%


.. index::
single: Templating; Including stylesheets and JavaScripts
Expand Down Expand Up @@ -1218,7 +1184,7 @@ automatically:

.. versionadded:: 2.6
The global ``app.security`` variable (or the ``$app->getSecurity()``
method in PHP templates) is deprecated as of Symfony 2.6. Use ``app.user``
method in PHP templates) is deprecated as of Symfony 2.6. Use ``app.user``
(``$app->getUser()``) and ``is_granted()`` (``$view['security']->isGranted()``)
instead.

Expand Down
5 changes: 1 addition & 4 deletions reference/configuration/framework.rst
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ templating
~~~~~~~~~~

.. _reference-templating-base-urls:
.. _ref-framework-assets-base-urls:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to be careful that this label is not removed when #5170 is merged.


assets_base_urls
................
Expand Down Expand Up @@ -971,10 +972,6 @@ Now, the same asset will be rendered as ``/images/logo.png?v2`` If you use
this feature, you **must** manually increment the ``assets_version`` value
before each deployment so that the query parameters change.

It's also possible to set the version value on an asset-by-asset basis (instead
of using the global version - e.g. ``v2`` - set here). See
:ref:`Versioning by Asset <book-templating-version-by-asset>` for details.

You can also control how the query string works via the `assets_version_format`_
option.

Expand Down
29 changes: 24 additions & 5 deletions reference/twig_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,12 @@ asset

.. code-block:: jinja

{{ asset(path, packageName, absolute = false, version = null) }}
{{ asset(path, packageName) }}

``path``
**type**: ``string``
``packageName``
**type**: ``string`` | ``null`` **default**: ``null``
``absolute``
**type**: ``boolean`` **default**: ``false``
``version``
**type**: ``string`` **default** ``null``

Returns a public path to ``path``, which takes into account the base path
set for the package and the URL path. More information in
Expand All @@ -126,6 +122,29 @@ assets_version
Returns the current version of the package, more information in
:ref:`book-templating-assets`.

absolute_url
~~~~~~~~~~~~

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a versionadded directive here.

.. code-block:: jinja

{{ absolute_url(asset(path, packageName)) }}

``path``
**type**: ``string``
``packageName``
**type**: ``string`` | ``null`` **default**: ``null``

Returns the absolute URL that corresponds to the given asset path and package.
More information in :ref:`book-templating-assets`. For configuring the base URLs,
:ref:`ref-framework-assets-base-urls`.

The absolute URLs generated with this function ignore the asset versioning.
Combine it with the ``assets_version()`` function to append the version number:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding a separate section for the assets_version() function?


.. code-block:: jinja

{{ absolute_url(asset('logo.png')) ~ '?' ~ assets_version('images') }}

form
~~~~

Expand Down