doumicheng6732 2016-02-19 12:41
浏览 461


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条回答 默认 最新

  • du0531 2016-02-19 12:52

    Using [] adds an element to an array and explode also creates an array. So now you have a 2-dimensional array.

    Simply change the code in your controller to

    $ingred = explode('-', $ingreds);
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



    • ¥15 请教一下simulink中S函数相关问题
    • ¥15 Hadoop中eclipse运行问题
    • ¥15 在二层网络中,掩码存在包含关系即可通信
    • ¥15 端口转发器解析失败不知道电脑设置了啥
    • ¥15 Latex算法流程图行号自定义
    • ¥15 关于#python#的问题:我在自己的电脑上运行起来总是报错,希望能给我一个详细的教程,(开发工具-github)
    • ¥40 基于51单片机实现球赛计分器功能
    • ¥15 cs2游戏画面卡住,应用程序sid与指挥者sid不匹配
    • ¥15 实验七:Pandas要有实验截图和代码
    • ¥15 TypeError: Make sure that the iterable only contains strings.