我恳请您帮助我在 Twig 模板中显示动态图像 URL,如下所示:
// non-working pseudocode, myImageFilename contains image filename
{% image '@AppBundle/Resources/public/images/' + myImageFilename %}
<img src="{{ asset_url }}" />
{% endimage %}
但是 Twig 只允许静态文件名,就像这样(取自文档)。我试过连接器 ~
就像
{% image '@AppBundle/Resources/public/images/' ~ myImageFilename %}
但是还是不行,
{% image '@AppBundle/Resources/public/images/example.jpg' %}
<img src="{{ asset_url }}" alt="Example"/>
{% endimage %}
是否可以将动态图像 URL 与此 Twig 图像逻辑一起使用?
最佳答案
目前,我认为这是不可能的。这背后的原因是 Assetic 预先运行以转储 Assets 。
可以通过这种方式:
<img src="{{ asset('bundles/appbundle/Resources/public/images/'~myImageFilename }}" alt="Example"/>
关于php - 如何使用 Twig 显示动态图像 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44700324/