doumicheng6732 2016-02-19 12:41
浏览 461
已采纳

在Twig模板中显示数组

Have problem to display an array in a Twig template.

In mysql I have a text field with value: testtest-testtest2-testtest3

In the controller I explode by '-' and pass it to Twig like so:

$ingred[] = explode('-', $ingreds);
$this->twig->render(['ingred' => $ingred]);

In the Twig template I loop through and it works:

   {% for ing in ingred %}
     <span class="ingridiantai">{{ ing.0 }}</span>
     <span class="ingridiantai">{{ ing.1 }}</span>
     <span class="ingridiantai">{{ ing.2 }}</span>
     <span class="ingridiantai">{{ ing.3 }}</span>
     <span class="ingridiantai">{{ ing.4 }}</span>
     <span class="ingridiantai">{{ ing.5 }}</span> 
   {% endfor %}

But what I want is to loop through all values without writing their key. Like this:

   {% for ing in ingred %}
     <span class="ingridiantai">{{ ing }}</span> 
   {% endfor %}

How can I do what? I was searching in the docs, but didn't find anything that would work for me.

  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 看看怎么编程?运用函数
      • ¥15 求解,老毛子skokina是什么东西,是否存在
      • ¥15 git回滚后怎么再恢复
      • ¥15 轴承故障诊断,CDAE之后加傅里叶变换FFT,然后输入到BiLSTM中去,这个傅里叶变换该怎么加。
      • ¥15 哪位硬件专家帮助分析下OPFC无输出 原因,电压都已标注出来,芯片是FAN4800A 。
      • ¥15 启动navicat时报10061错
      • ¥20 关于#pcb工艺#的问题:只需要设计图和设计原理
      • ¥15 关于#gstreamer webrtcbin#的问题,如何解决?
      • ¥15 怎么用c语言函数编写宿舍财务管理系统?
      • ¥15 css网页样式与布局