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