Php 如何从Twig中的变量加载图像?(使用AsseticBundler)

Php 如何从Twig中的变量加载图像?(使用AsseticBundler),php,symfony,twig,symfony-3.4,assetic,Php,Symfony,Twig,Symfony 3.4,Assetic,我正在尝试循环遍历一个带有字符串的数组,并在图像源路径中使用这些字符串。这样,我可以为每个循环显示不同的图像 这是一段可以工作的代码,但只适用于一组字符串,这意味着我不能以这种方式使其过于动态: {% image 'img/appel ijs.jpg' %} <img class="recept-image-custom" src="{{ asset_url }}"/> {% endimage %} {%image'img/appel ijs.jpg%} {%endimag

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

这是一段可以工作的代码,但只适用于一组字符串,这意味着我不能以这种方式使其过于动态:

{% image 'img/appel ijs.jpg' %}
    <img class="recept-image-custom" src="{{ asset_url }}"/>
{% endimage %}
{%image'img/appel ijs.jpg%}
{%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 %}
{items%]中的项的%
{%image('img/'~item.name~'.jpg')%}
{%endimage%}
{%endfor%}
当我执行此操作时,会出现以下错误:
值的意外标记“标点符号”(“

请注意,我对twig还是一个新手,如果这完全是错误的,请原谅。我已经尝试了asset(“”),但这样我在运行服务器时无法访问我的web文件夹。

这样如何:

{% for item in items %}
    <tr>
        <td scope="col">
            <img class="recept-image-custom" src="{{ asset('img/' ~ item.name ~ '.jpg') }}"/>
        <td>
    </tr>
{% endfor %}
{items%]中的项的%
{%endfor%}
这个怎么样:

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

试试这个:
{%set img='img/'~item.name~'.jpg%}{%image img%}….{%endimage%}
嗨,我以前试过,这给了我以下一个意外的标记错误。这是我尝试你的建议时得到的:
值为“img”的意外标记“name”
我想你只能在那里手动输入字符串。很奇怪,我不知道该怎么办…看。这确实是不可能的,你需要求助于
asset()
啊,好吧,那很不幸。谢谢你!试试这个:
{%set img='img/'~item.name~'.jpg%}{%image img%}..{%endimage%}
Hi,我以前尝试过,这给了我以下一个意外的令牌错误。这是我尝试您的建议时得到的:
值为“img”的意外令牌“name”
我假设您只能在其中手动输入字符串。非常奇怪,我不知道该怎么办…请看。这确实不可能,您需要求助于
资产()
啊,好吧,这很不幸。不过谢谢你!我这样做了,而且只有当我从本地主机手动转到我的php文件时,它才起作用。当我运行
php bin/console服务器时:运行
它不起作用。不知怎的,assetic bundler无法从我的web文件夹加载图像。你发布的解决方案就是我现在使用的备用方案。我可能会错过它hahaI做了一些事情,只有当我从本地主机手动转到我的php文件时,它才起作用。当我运行
php bin/console服务器时:运行
它不起作用。不知怎的,assetic bundler无法从我的web文件夹加载图像。你发布的解决方案就是我现在使用的备用方案。我可能遗漏了什么,哈哈