Php 将数组传递给Symfony中的细枝宏
我有一个控制器,一个细枝宏和主模板,在那里宏被导入。我想在我的宏中发送一个数组,我想在表的每一行显示它的数据 一行由以下内容组成:标题、描述、img、类型 控制器:Php 将数组传递给Symfony中的细枝宏,php,symfony,twig,Php,Symfony,Twig,我有一个控制器,一个细枝宏和主模板,在那里宏被导入。我想在我的宏中发送一个数组,我想在表的每一行显示它的数据 一行由以下内容组成:标题、描述、img、类型 控制器: /** * @Route("/realisations", name="realisations") * @Method({"GET"}) * * @return \Symfony\Component\HttpFoundation\Response */ public function realisationsAction
/**
* @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 %}
主模板:
/**
* @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 %}
宏:
/**
* @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 %}
{%宏实现(数据)%}
{%用于数据%中的数据}
{{data.titre}
{{data.description}}
{%endfor%}
{%endmacro%}
符号错误为:“变量”数据“不存在。”
我认为错误来自于在控制器中创建我的表,但我不知道如何正确创建它
提前感谢只需传递一个带有名为“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);
希望这有帮助