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

在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.

图片转代码服务由CSDN问答提供 功能建议

在Twig模板中显示数组时出现问题。

在mysql中 我有一个值为的文本字段: testtest-testtest2-testtest3

在控制器中我按' - '爆炸并将其传递给Twig,如下所示:

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

在Twig模板中,我循环完成它的工作原理:

  {%for ing  in ingred%} 
&lt; span class =“ingridiantai”&gt; {{ing.0}}&lt; / span&gt; 
&lt; span class =“ingridiantai”&gt; {{ing.1}}&lt; /  span&gt; 
&lt; span class =“ingridiantai”&gt; {{ing.2}}&lt; / span&gt; 
&lt; span class =“ingridiantai”&gt; {{ing.3}}&lt; / span&gt;  
&lt; span class =“ingridiantai”&gt; {{ing.4}}&lt; / span&gt; 
&lt; span class =“ingridiantai”&gt; {{ing.5}}&lt; / span&gt;  
 {%endfor%} 
   
 
 

但我想要的是在不写入密钥的情况下遍历所有值。 像这样:

  {%for ing in ingred%} 
&lt; span class =“ingridiantai”&gt; {{ing}}&lt; / span&gt;  
 {%endfor%} 
   
 
 

我该怎么办? 我在文档中搜索,但没有发现任何对我有用的东西。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题