我想分享一些變量分成不同的樹枝模板:分享同一個變量多個模板TWIG/Symfony的
這裏是我的控制器:
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))''在你的控制器中? – yceruto
對不起,還沒有定義... –
您能否顯示異常的截圖? – yceruto