【发布时间】:2016-09-02 15:39:24
【问题描述】:
我想将一些变量分享到不同的树枝模板中:
这是我的控制器:
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
//Get All projects
$projects = $em->getRepository('WebAwardsBundle:Project')->findAll();
//Get the Winner of the day
$winner = $em->getRepository('WebAwardsBundle:Winner')->findBy(
array('isDay' => '1')
);
foreach($winner as $win){
$idProject = $win->getIdProject();
}
$winner = $em->getRepository('WebAwardsBundle:Project')->findById($idProject);
//Get the author of the project
$idUser = $winner[0]->getIdAuthor();
$user = $em->getRepository('WebAwardsBundle:User')->findById($idUser);
//Get the vote of the project
$vote = $em->getRepository('WebAwardsBundle:Vote')->findByIdProject($idProject);
//Get the last project of the Month
//All Winner of the month
//Recuperer dans la liste de tous les projets, le projet == meme id, order by date desc limit 1
return $this->render('project/index.html.twig', array(
'projects' => $projects,
'winner' => $winner,
'user' => $user,
'vote' => $vote,
));
}
base.html.twig 中有一个“赢家”对象:
<footer class="footer">
<div class="container-fluid">
<div class="col-xs-12 col-md-4">
<div class="col-xs-12">- SITE DU MOIS-</div>
<div class="col-xs-12">{{ winner[0].name }}</div>
<div class="col-xs-12">- SITE DU MOIS-</div>
</div>
<div class="col-xs-12 col-md-4">qsd</div>
<div class="col-xs-12 col-md-4">dqs</div>
<div class="row">
<div class="col-xs-12">
<p class="text-center">© Copyright 2016 by <a href="{{ path('homepage') }}">WebAwards</a>.</p>
</div>
</div>
</div>
</footer>
这里是我需要获胜者对象的 login.html.twig:
{% block footer %}
<!-- WINNER IN UNDEFINED :'( -->
{{ include('base.html.twig', {'winner': winner }) }}
{% endblock %}
大家都明白哪里不对了吗?
【问题讨论】:
-
你确定
winner被传递给login.html.twig模板吗?像$this->render('login.html.twig', array('winner' => $winner))在你的控制器中? -
是的,抱歉,仍未定义...
-
能否请您显示异常的屏幕截图?
-
@Yonel 不确定您要通过编辑修复什么,但是以这种方式完全删除一大块代码看起来真的很糟糕。你是不是想歪曲这个问题?