blob: e008266b346c7b2be99ce1f19449f6ce11e1ad8c [file] [log] [blame]
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01001``data_uri``
2============
3
4The ``data_uri`` filter generates a URL using the data scheme as defined in
5`RFC 2397`_:
6
7.. code-block:: html+twig
8
9 {{ image_data|data_uri }}
10
11 {{ source('path_to_image')|data_uri }}
12
13 {# force the mime type, disable the guessing of the mime type #}
14 {{ image_data|data_uri(mime="image/svg") }}
15
16 {# also works with plain text #}
17 {{ '<b>foobar</b>'|data_uri(mime="text/html") }}
18
19 {# add some extra parameters #}
20 {{ '<b>foobar</b>'|data_uri(mime="text/html", parameters={charset: "ascii"}) }}
21
22.. note::
23
24 The ``data_uri`` filter is part of the ``HtmlExtension`` which is not
25 installed by default. Install it first:
26
27 .. code-block:: bash
28
29 $ composer require twig/html-extra
30
31 Then, on Symfony projects, install the ``twig/extra-bundle``:
32
33 .. code-block:: bash
34
35 $ composer require twig/extra-bundle
36
37 Otherwise, add the extension explicitly on the Twig environment::
38
39 use Twig\Extra\Html\HtmlExtension;
40
41 $twig = new \Twig\Environment(...);
42 $twig->addExtension(new HtmlExtension());
43
44.. note::
45
46 The filter does not perform any length validation on purpose (limit depends
47 on the usage context), validation should be done before calling this filter.
48
49Arguments
50---------
51
52* ``mime``: The mime type
53* ``parameters``: An array of parameters
54
55.. _RFC 2397: https://tools.ietf.org/html/rfc2397