如何在 twig 模板中加载 css 文件中的图像?

how to have image in css file that is loaded in a twig template?

在树枝模板中,我将其用于图像:

{% image '@AcmeFooBundle/Resources/public/images/example.jpg' %}
    <img src="{{ asset_url }}" alt="Example" />
{% endimage %}

这是 css:

{% stylesheets '@AppBundle/Resources/public/css/*' %}
   <link rel="stylesheet" href="{{ asset_url }}" type="text/css" />
{% endstylesheets %}

但在我的 css 中,我有一些 类 图片:

background-image: url(../images/left_menu_bg.jpg);

当然是图片坏了,请问有什么办法可以解决吗?或者我需要以任何方式将背景图像移动到树枝模板?如果是的话,我怎样才能有 div 的背景图像?

<div class="blah" background="image.jpg">

我认为完整路径不像:

background-image: url(Acme/AcmeDemo/images/left_menu_bg.jpg);

因为 URI 中的查询参数会有帮助吗?

您应该在样式表标签内使用 cssrewrite 过滤器:

来自 symfony 文档:http://symfony.com/doc/current/cookbook/assetic/asset_management.html#cookbook-assetic-including-css

But because Assetic changes the paths to your assets, this will break any background images (or other paths) that uses relative paths, unless you use the cssrewrite filter.

{% stylesheets 'bundles/acme_foo/css/*' filter='cssrewrite' %}
    <link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}