A.Sami
A.Sami

Reputation: 51

Share same variable multiple templates TWIG/SYMFONY

I would like share some variables into different twig template:

Here's my controller:

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,
    ));
}

There's a "winner" object into 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">&copy; Copyright 2016 by <a href="{{ path('homepage') }}">WebAwards</a>.</p>
            </div>
        </div>
    </div>
</footer>

And here is the login.html.twig where I need the Winner Object:

{% block footer %}
    <!-- WINNER IN UNDEFINED :'( -->
    {{ include('base.html.twig', {'winner': winner }) }}
{% endblock %}

Does everyone see what's wrong?

Upvotes: 0

Views: 824

Answers (2)

A.Sami
A.Sami

Reputation: 51

I fix my trouble with :

  • Create the WinnerDay function in the Winner Controller :

    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
    ));` 
    
  • Call the action in the the parent template (base.html.twig)

    `{{ render(controller('WebAwardsBundle:Winner:winnerDay' )) }}`
    
  • Finally I created a footer.html.twig where the variable is now available

Upvotes: 0

Alvin Bunk
Alvin Bunk

Reputation: 7764

It looks like you are defining $winner twice, take a look:

$winner = $em->getRepository('WebAwardsBundle:Winner')->findBy(
  array('isDay' => '1')
);
...
$winner = $em->getRepository('WebAwardsBundle:Project')->findById($idProject);

This definitely doesn't look right!

Upvotes: 1

Related Questions