簡體   English   中英

將數組傳遞給Symfony中的樹枝宏

[英]Pass an array to a twig macro in 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錯誤是: “變量”數據“不存在”。

我認為錯誤來自在控制器中創建我的表,但我不知道如何正確創建它

提前致謝

只需傳遞一個名為'datas'的鍵的數組作為示例:

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

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

希望這有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM