首页 > 解决方案 > 如何从 Twig 中的变量加载图像?(使用 AsseticBundler)

问题描述

我正在尝试遍历带有字符串的数组并在我的图像源路径中使用这些字符串。这样我就可以为每个循环显示不同的图像。

这是一段有效的代码,但只能使用一组字符串,这意味着我不能以这种方式使这一切变得过于动态:

{% image 'img/appel ijs.jpg' %}
    <img class="recept-image-custom" src="{{ asset_url }}"/>
{% endimage %}

这是我尝试过的:

{% for item in items %}
    <tr>
        <td scope="col">
            {% image ('img/' ~ item.name ~ '.jpg') %}
                <img class="recept-image-custom" src="{{ asset_url }}"/>
            {% endimage %}
        <td>
    </tr>
{% endfor %}

当我这样做时,我收到以下错误: Unexpected token "punctuation" of value "("

请注意,我对树枝还是很陌生,如果这完全错误,请原谅。我已经尝试过asset(''),但是这样我在运行服务器时无法访问我的网络文件夹。

标签: phpsymfonytwigsymfony-3.4assetic

解决方案


这个怎么样 :

{% for item in items %}
    <tr>
        <td scope="col">
            <img class="recept-image-custom" src="{{ asset('img/' ~ item.name ~ '.jpg') }}"/>
        <td>
    </tr>
{% endfor %}

推荐阅读