data_uri
¶
这个 data_uri
过滤使用中定义的数据方案生成网址 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"}) }}
|
注解
这个 data_uri
过滤器是 HtmlExtension
默认情况下不安装。先安装:
1 | $ composer require twig/html-extra
|
然后,在symfony项目上,安装 twig/extra-bundle
:
1 | $ composer require twig/extra-bundle
|
否则,请在Twig环境中显式添加扩展::
use Twig\Extra\Html\HtmlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new HtmlExtension());
注解
过滤不会刻意进行长度校验(限制取决于使用上下文),需要先校验后再调用过滤。
争论¶
mime
:mime类型parameters
:参数数组