File: data_uri.rst

package info (click to toggle)
php-twig 2.14.3-1%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,240 kB
  • sloc: php: 18,830; makefile: 215; sh: 42; python: 33
file content (52 lines) | stat: -rw-r--r-- 1,347 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
``data_uri``
============

.. versionadded:: 2.12

    The ``data_uri`` filter was added in Twig 2.12.

The ``data_uri`` filter generates a URL using the data scheme as defined in RFC
2397:

.. code-block:: html+twig

    {{ image_data|data_uri }}

    {{ source('path_to_image')|data_uri }}

    {# force the mime type, disable the guessing of the mime type #}
    {{ image_data|data_uri(mime="image/svg") }}

    {# also works with plain text #}
    {{ '<b>foobar</b>'|data_uri(mime="text/html") }}

    {# add some extra parameters #}
    {{ '<b>foobar</b>'|data_uri(mime="text/html", parameters={charset: "ascii"}) }}

.. note::

    The ``data_uri`` filter is part of the ``HtmlExtension`` which is not
    installed by default. Install it first:

    .. code-block:: bash

        $ composer require twig/html-extra

    Then, use the ``twig/extra-bundle`` on Symfony projects or add the extension
    explicitly on the Twig environment::

        use Twig\Extra\Html\HtmlExtension;

        $twig = new \Twig\Environment(...);
        $twig->addExtension(new HtmlExtension());

.. note::

    The filter does not perform any length validation on purpose (limits depends
    on the usage context), validation should be done before calling this filter.

Arguments
---------

* ``mime``: The mime type
* ``parameters``: An array of parameters