【问题标题】:Pass an array to a twig macro in Symfony将数组传递给 Symfony 中的树枝宏
【发布时间】:2017-04-20 15:28:48
【问题描述】:

我有一个控制器、一个树枝宏和导入宏的主模板。我想在我的宏中发送一个数组,并为我表的每一行显示它的数据。

一行由:title、description、img、type组成

控制器:

/**
 * @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 错误是:“变量“数据”不存在。”

我认为错误来自在控制器中创建我的表,但我不知道如何正确创建它

提前致谢

【问题讨论】:

    标签: php symfony twig


    【解决方案1】:

    只需传递一个带有名为 '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);
    

    希望有帮助

    【讨论】:

      猜你喜欢
      • 2016-02-20
      • 2020-02-05
      • 2016-07-17
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      • 1970-01-01
      • 2012-12-26
      • 1970-01-01
      相关资源
      最近更新 更多