php - 如何从 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(''),但是这样我在运行服务器时无法访问我的网络文件夹。
解决方案
这个怎么样 :
{% for item in items %}
<tr>
<td scope="col">
<img class="recept-image-custom" src="{{ asset('img/' ~ item.name ~ '.jpg') }}"/>
<td>
</tr>
{% endfor %}
推荐阅读
- django - Chart.js - 多个条形图 - 仅显示最后一个图表
- apache-kafka - Debezium,Kafka 连接:有没有办法只发送有效负载而不发送模式?
- python-3.x - 为什么它显示总和为 0?
- c# - C#,如何从 Async/Await 任务返回字符串作为结果
- python - 如何将代码变成单行返回语句
- .net-core - 轨道交通。编辑 MessageType 属性以删除不必要的项目
- swift - 在 UIImageView 上设置图像会改变动画
- amcharts - 仅在分组工具提示中显示活动系列(amcharts)
- java - Docker 运行返回异常:应用程序已由更新版本的 Java 运行时编译
- c# - 哪个内置 C# 异常用于检查列表中的已删除项目