data_uri
data_uri
过滤器使用 RFC 2397 中定义的数据方案生成 URL
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"}) }}
注意
data_uri
过滤器是默认未安装的 HtmlExtension
的一部分。请先安装它
1
$ composer require twig/html-extra
然后,在 Symfony 项目上,安装 twig/extra-bundle
1
$ composer require twig/extra-bundle
否则,在 Twig 环境中显式添加扩展
1 2 3 4
use Twig\Extra\Html\HtmlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new HtmlExtension());
注意
该过滤器没有特意执行任何长度验证(限制取决于使用上下文),验证应在调用此过滤器之前完成。
参数
mime
: MIME 类型parameters
: 参数映射