我想将一些变量分享到不同的树枝模板中:
这是我的控制人员:
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>
这是我需要Winner对象的login.html.twig:
{% block footer %}
<!-- WINNER IN UNDEFINED :'( -->
{{ include('base.html.twig', {'winner': winner }) }}
{% endblock %}
每个人都看错了吗?
答案 0 :(得分:0)
看起来你要两次定义$ winner,看看:
$winner = $em->getRepository('WebAwardsBundle:Winner')->findBy(
array('isDay' => '1')
);
...
$winner = $em->getRepository('WebAwardsBundle:Project')->findById($idProject);
这绝对不合适!
答案 1 :(得分:0)
我解决了我的麻烦:
在Winner Controller中创建WinnerDay函数:
// Ajax POST: /Checkout/UseShippingAddress/5
[HttpPost]
public ActionResult UseShippingAddress(int id)
{
return Content("It worked!");
}
在父模板(base.html.twig)中调用操作
public function winnerDayAction(){
$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);
$userId = $winner[0]->getIdAuthor();
$user = $em->getRepository('WebAwardsBundle:User')->findById($userId);
return $this->render('footer.html.twig', array(
'winner' => $winner,
'user' => $user
));`
最后我创建了一个footer.html.twig,变量现在可用了