我使用的代码点火器与Twig -我试图创建一个动态的选择框,其中包括来自数据库的数据。
以下是我现在正在做的事情
// 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>所以这一切都很好。但如果我想让它充满活力呢?每个复选框都从控制器中的不同方法中获取数据--假设我有来自其他方法的数据--如下所示
// 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>我的想法是做这样的事:
设置控制器名称的数组:
{% set controller_names = ['animals','orders']%}设置控制器变量的数组及其键:
{% set controller_vars =
['animals'=>'animal_description','orders'=>'order_description']%}然后像这样反复迭代
{% 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 %} 因此,我需要的是将集合controller_vars数组转换为Twig变量(尽可能多).
发布于 2017-04-26 07:46:22
您可以使用属性函数:
{% for controller_key in controller_vars %}
<option >
{{ attribute(controller_key, controller_vars) }}
</option>
{% endfor %}希望能帮上忙
https://stackoverflow.com/questions/43627267
复制相似问题