2017-04-26 1 views
1

저는 Codeigiter와 Twig를 사용하고 있습니다. 데이터베이스의 데이터로 구성된 동적 선택 상자를 만들려고합니다. 여기Twig 배열에서 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']%} 

그런 다음 당신이 할 수있는

답변

0

..... 내가 변환이 (같은 그것이 가능한) 변수를 나뭇 가지 할 수있는 설정 controller_vars 배열입니다 필요 그래서

  {% 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 %} 

처럼 반복

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

희망이 도움

+0

목 다음 attribute 기능을 사용 anks -하지만 작동하지 않습니다 ... 제발 확장 예제를 주실 수 있습니까? – RoyBarOn

+0

안녕하세요 @RoyBarOn 죄송합니다. 예제에서 실수를했습니다. 속성 함수의 매개 변수를 뒤집습니다 (답변을 업데이트합니다). 시도해 볼 수 있습니까? – Matteo

+0

죄송합니다 - 작동하지 않습니다 - 키와 값이 PHP 변수라는 것을 나뭇 가지에 "말하려고합니다."... – RoyBarOn

관련 문제