dousuie2222 2017-04-26 07:08
浏览 59

从Twig数组创建Twig变量

I'm using codeigniter with Twig - i'm trying to create a dynamic select boxes which consist of data from data base.

Here is what i'm doing now

      // animals is $data['animals']=$this->loadmodel->some_function()
      // animals passed from the controller ...


           <select  id="foo" class="form-control">
                <option selected="true" >Animals</option>
                   {%  for animal_key in animals %}
                      <option >
                     {{ animal_key ["animal_description"] }}
                     </option>
                   {% endfor %}
             </select>

So all above is working well. But what if i want to make it dynamic? each select box get data from different method in the controller - suppose i have data from other method - like here

       // orders is $data['orders']=$this->loadmodel->some_function()
       // orders passed from the controller ...

           <select  id="foo" class="form-control">
                <option selected="true" >orders</option>
                   {%  for order_key in orders%}
                      <option >
                     {{ order_key ["order_description"] }}
                     </option>
                   {% endfor %}
             </select>

             <select  id="foo" class="form-control">
                <option selected="true" >Animals</option>
                   {%  for animal_key in animals %}
                      <option >
                     {{ animal_key ["animal_description"] }}
                     </option>
                   {% endfor %}
             </select>

What i had in mind was to do something like this :

Set an array of the controller's names :

         {% set controller_names = ['animals','orders']%}

Set an array of the controllers variables a nd their keys :

         {% set controller_vars = 
           ['animals'=>'animal_description','orders'=>'order_description']%}

Then iterate over it like that

         {%  for names in controller_names  %}
              <select  id="foo" class="form-control">
                <option selected="true" >{{ name }}</option>
                   {%  for controller_key in controller_vars %}
                      <option >
                     {{ controller_vars [ controller_key] }} //suppose to be Twig variables
                     </option>
                   {% endfor %}
             </select>
         {% endfor %} 

So what i need is the convert the set controller_vars array to Twig variables (as long as it possible).....

  • 写回答

1条回答 默认 最新

  • doujia6433 2017-04-26 07:46
    关注

    You can use the attribute function:

                   {%  for controller_key in controller_vars %}
                      <option >
                        {{ attribute(controller_key, controller_vars) }}
                     </option>
                   {% endfor %}
    

    Hope this help

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c