Variable "results" does not exist – in Twig from an Embedded Controller

I am using symfony5 with Twig to render my pages.

I have a page on which i receive content from my search bar. This search bar comes from an embedded controller that I generate in my current page with this {{ render(path('navBar')) }} .

The issue is that one of my variables for the search bar goes in error Variable "results" does not exist. . I assure you it exists in my embedded controller, and that it is defined (I tried deleting it, I then get an Variable "results" is not defined. error.)

So… Symfony understands it is alive somewhere, but why would it say it does not exist ?
Here is my code, thank you all.

Here is my Twig template on which I have an error. The rest of the template isn’t source of error, so I didn’t copy it here.

<div>
    {{ render(path('navBar')) }}
</div>
<main>
    <section class="container-fluid mx-2">
        <!-- middle section flex div-->     
        <div id="middle_section">
            <!-- left block for filters -->
            <div id="filter_card" class="card col-sm-2">
                {{ render(path('filter')) }}
            </div>
            <!-- music card div-->
            <div class="col-sm-9 px-0">
                {% if app.request.method == 'POST' %}
                    {% if results |length > 0 %}
                    <h4 class="search_found_nothing">Aucun mix contenant ce mot clé dans le titre n'a été trouvé, essayez en un autre.</h4>
                    {% else %}
                    <h4>Résultats pour votre recherche</h4>
                    {% for info in results %}
                    <div class="card mix_card my-3">
                        <div class="col-xl-3 thumbnail">
                            <img class="mix_thumbnail" src="{{ asset('images/' ~ info.thumbnail ~ '') }}" alt="{{ info.thumbnaildescription }}">
                        </div>
                        <div class="col-xl-9 ">
                            <!-- music info div -->
                            <div class="d-flex justify-content-row flex-wrap">
                                <h4 class="mix_title">{{ info.title }}</h4>
                                <h5 class="mix_genre">{{ info.genre }}</h5>
                                <h5 class="mix_duration">{{ info.duration }} min</h5>
                                <div class="fb-share-button mix_facebook" data-href="https://www.facebook.com/DJ.Demoniack" data-layout="button_count" data-size="small"><a target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.facebook.com%2FDJ.Demoniack&amp;src=sdkpreparse" class="fb-xfbml-parse-ignore">Partager</a></div>
                                <a target="blank" href="{{ path('mix_page', {'id': info.id}) }}" data-toggle="tooltip" data-placement="top" title="Ouvrir le mix dans une autre page" class="mix_share"><i class="fas fa-external-link-alt"></i></a>
                                <a href="{{ asset('mix/' ~ info.filename ~ '') }}" data-toggle="tooltip" data-placement="top" title="Télécharger gratuitement le mix"  id="mix_download"><i class="fas fa-download"></i></a>
                            </div>           
                            <!-- music player div -->
                            <div class="container-audio my-2">
                                <audio id="audioMix" controls seekable loop>
                                    <source src="{{ asset('mix/test.MP3') }}" type="audio/ogg">
                                            Your browser dose not Support the audio Tag
                                </audio>
                            </div>
                        </div>
                    {% endfor %}
                    {% endif %}
                    </div>
                {% else %}

Here is my embedded controller :

class DefaultController extends AbstractController
{
    /**
     * @Route("/navBar", name="navBar")
    */
    public function navBar(SongRepository $song_repository, Request $request): Response
    {
    
    $results = [];

    $searchMixForm = $this->createForm(SearchMixType::class);

        if($searchMixForm->handleRequest($request)->isSubmitted() && $searchMixForm->isValid()) {
            $title = $searchMixForm->getData();

            $results = $song_repository->searchMix($title);

            if ($results == null) {
                $this->addFlash('erreur', 'Aucun mix contenant ce mot clé dans le titre n'a été trouvé, essayez en un autre.');
                }
        }  
        return $this->render('default/_nav.html.twig', [
            "search_form" => $searchMixForm->createView(),
            "results" => $results,
        ]);
    }

And finally, here is the controller of the current template :

class SongController extends AbstractController
{
    /**
     * @Route("/mixes/{id}", name="mix_page", requirements={"id"="d+"}))
     */
    public function mixById($id, SongRepository $song_repository)
    {
        $songById = $song_repository->viewById($id);
        return $this->render('song/mix.html.twig', [
            "songById" => $songById,
        ]);
    }
}

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *