Twig

The flexible, fast, and secure
template engine for PHP

a Symfony Product
You are reading the documentation for Twig 2.x. Switch to the documentation for Twig 1.x. 3.x.

Table of Contents

Questions & Feedback

License

Twig documentation is licensed under the new BSD license.

data_uri

New in version 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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{{ 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:

1
$ composer req twig/html-extra

Then, use the twig/extra-bundle on Symfony projects or add the extension explictly 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
Website powered by Symfony and Twig, deployed on
The Twig logo is © 2010-2020 Symfony