Skip to content

Commit 5a0775a

Browse files
committed
[Twig][twig reference] add examples to functions and filter
1 parent e134083 commit 5a0775a

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

Diff for: reference/twig_reference.rst

+92
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ asset
114114
``packageName`` *(optional)*
115115
**type**: ``string`` | ``null`` **default**: ``null``
116116

117+
.. code-block:: yaml
118+
119+
# config/packages/framework.yaml
120+
framework:
121+
# ...
122+
assets:
123+
packages:
124+
foo_package:
125+
base_path: /avatars
126+
127+
.. code-block:: twig
128+
129+
{# the image lives at "public/avatars/avatar.png" #}
130+
{{ asset(path = 'avatar.png', packageName = 'foo_package') }}
131+
{# output: /avatars/avatar.png #}
132+
117133
Returns the public path of the given asset path (which can be a CSS file, a
118134
JavaScript file, an image path, etc.). This function takes into account where
119135
the application is installed (e.g. in case the project is accessed in a host
@@ -189,6 +205,30 @@ logout_path
189205
Generates a relative logout URL for the given firewall. If no key is provided,
190206
the URL is generated for the current firewall the user is logged into.
191207

208+
.. code-block:: yaml
209+
210+
# config/packages/security.yaml
211+
security:
212+
# ...
213+
214+
firewalls:
215+
main:
216+
# ...
217+
logout:
218+
path: '/logout'
219+
othername:
220+
# ...
221+
logout:
222+
path: '/other/logout'
223+
224+
.. code-block:: twig
225+
226+
{{ logout_path(key = 'main') }}
227+
{# output: /logout #}
228+
229+
{{ logout_path(key = 'othername') }}
230+
{# output: /other/logout #}
231+
192232
logout_url
193233
~~~~~~~~~~
194234

@@ -202,6 +242,30 @@ logout_url
202242
Equal to the `logout_path`_ function, but it'll generate an absolute URL
203243
instead of a relative one.
204244

245+
.. code-block:: yaml
246+
247+
# config/packages/security.yaml
248+
security:
249+
# ...
250+
251+
firewalls:
252+
main:
253+
# ...
254+
logout:
255+
path: '/logout'
256+
othername:
257+
# ...
258+
logout:
259+
path: '/other/logout'
260+
261+
.. code-block:: twig
262+
263+
{{ logout_url(key = 'main') }}
264+
{# output: http://example.org/logout #}
265+
266+
{{ logout_url(key = 'othername') }}
267+
{# output: http://example.org/other/logout #}
268+
205269
path
206270
~~~~
207271

@@ -219,6 +283,14 @@ path
219283
Returns the relative URL (without the scheme and host) for the given route.
220284
If ``relative`` is enabled, it'll create a path relative to the current path.
221285

286+
.. code-block:: twig
287+
288+
{{ path(name = 'app_blog', parameters = {page: 3}, relative = false) }}
289+
{# output (depending on the route configuration): /blog/3 or /blog?page=3 #}
290+
291+
{{ path(name = 'app_blog', parameters = {page: 3}, relative = true) }}
292+
{# output (depending on the route configuration): blog/3 or ?page=3 #}
293+
222294
.. seealso::
223295

224296
Read more about :doc:`Symfony routing </routing>` and about
@@ -241,6 +313,16 @@ url
241313
Returns the absolute URL (with scheme and host) for the given route. If
242314
``schemeRelative`` is enabled, it'll create a scheme-relative URL.
243315

316+
.. code-block:: twig
317+
318+
{{ url(name = 'app_blog', parameters = {page: 3}, schemeRelative = false) }}
319+
{# output (depending on the route configuration): http://example.org/blog/3
320+
or http://example.org/blog?page=3 #}
321+
322+
{{ url(name = 'app_blog', parameters = {page: 3}, schemeRelative = true) }}
323+
{# output (depending on the route configuration): //example.org/blog/3
324+
or //example.org/blog?page=3 #}
325+
244326
.. seealso::
245327

246328
Read more about :doc:`Symfony routing </routing>` and about
@@ -292,6 +374,11 @@ expression
292374
Creates an :class:`Symfony\\Component\\ExpressionLanguage\\Expression` related
293375
to the :doc:`ExpressionLanguage component </components/expression_language>`.
294376

377+
.. code-block:: twig
378+
379+
{{ expression(1 + 2) }}
380+
{# output: 3 #}
381+
295382
impersonation_exit_path
296383
~~~~~~~~~~~~~~~~~~~~~~~
297384

@@ -577,6 +664,11 @@ file_link
577664
Generates a link to the provided file and line number using
578665
a preconfigured scheme.
579666

667+
.. code-block:: twig
668+
669+
{{ 'path/to/file/file.txt'|file_link(line = 3) }}
670+
{# output: file://path/to/file/file.txt#L3 #}
671+
580672
file_relative
581673
~~~~~~~~~~~~~
582674

0 commit comments

Comments
 (0)