[英]Pass an array to a twig macro in Symfony
I have a controller, a twig macro and the main template where the macro is imported. 我有一个控制器,一个树枝宏和导入宏的主模板。 I would like to send an array in my macro and for each row of my table I want to display its data.
我想在我的宏中发送一个数组,并且对于我的表的每一行,我想显示它的数据。
A line is composed of : title, description, img, type 一行由以下内容组成:标题,描述,img,类型
The controller : 控制器:
/**
* @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'));
}
The main template : 主要模板:
{% import "MyBundle:macros:realisationsMacros.html.twig" as macros %}
{{ macros.realisations(datas) }}
The macro : 宏:
{% 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 %}
The Symfony error is : "Variable "datas" does not exist." Symfony错误是: “变量”数据“不存在”。
I think the error comes from creating my table in the controller but I do not know how to create it correctly 我认为错误来自在控制器中创建我的表,但我不知道如何正确创建它
Thanks in advance 提前致谢
simply pass an array with a key named 'datas' as example: 只需传递一个名为'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);
Hope this help 希望这有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.