2017-04-20 46 views
2

我有一個控制器,一個樹枝宏和導入宏的主模板。我想發送一個數組在我的宏和我的表中的每一行我想顯示其數據。將數組傳遞給Symfony中的一個樹枝宏

甲線的組成如下:標題,說明,IMG型

控制器:

/** 
* @Route("/realisations", name="realisations") 
* @Method({"GET"}) 
* 
* @return \Symfony\Component\HttpFoundation\Response 
*/ 
public function realisationsAction() 
{ 
    return $this->render('MyBundle:page:realisations.html.twig', $datas = array('type' => 'The type', 
                       'img' => 'test.jpg', 
                       'titre' => 'Test', 
                       'description' => 'The description')); 
} 

主要模板:

{% import "MyBundle:macros:realisationsMacros.html.twig" as macros %} 

{{ macros.realisations(datas) }} 

宏:

{% macro realisations(datas) %} 
    {% for data in datas %} 
     <div class="project {{ data.type }} isotope-item" style="position: absolute; left: 0; top: 0; transform: translate3d(0px, 0px, 0px);"> 
      <div class="content"> 
       <div class="image"> 
        <img src="{{ asset('bundles/jsmetallerie/img/realisations/'~ data.type ~'/13.png') }}" alt="{{ data.titre }}"> 
        <div class="item-hover"> 
         <ul class="item-icons"> 
          <li><a href="{{ asset('bundles/jsmetallerie/img/realisations/'~ data.type ~'/'~ img) }}" data-fancybox><i class="fa fa-search"></i></a></li> 
          <li><a href="{{ path('jsm_realisation_projet') }}"><i class="fa fa-link"></i></a></li> 
         </ul> 
        </div> 
       </div> 
       <span class="h6">{{ data.titre }}</span> 
       <p>{{ data.description }}</p> 
      </div> 
     </div> 
    {% endfor %} 
{% endmacro %} 

Symfony錯誤是:「變量」數據「不存在」。

我認爲錯誤來自於控制器創建我的表,但我不知道如何正確地創建它提前

感謝

回答

3

簡單地傳遞一個陣列,一個名爲「數據」關鍵例如:

$params = array(
     'datas' => array('type' => 'The type', 
         'img' => 'test.jpg', 
         'titre' => 'Test', 
         'description' => 'The description') 
    ); 

    return $this->render('MyBundle:page:realisations.html.twig', $params); 

希望這有助於

+0

由於它的作品! :d –