codewithdary / symfony6-static-movies

Static frontend of my Symfony 6 CRUD application
34 stars 31 forks source link

Variable "movies" does not exist. #2

Open deva-max opened 1 year ago

deva-max commented 1 year ago

Variable "movies" does not exist. ExceptionStack Trace Twig\Error\ RuntimeError in E:\dev\htdocs_link\movies\templates\movies\index.html.twig (line 7) {% block body %}


Movies Review ({{ movies|length }})

symfony error

Created by Code With Dary

MoviesController.php <?php

namespace App\Controller;

use App\Entity\Movie; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route;

class MoviesController extends AbstractController {

#[Route('/movies', name: 'movies')]
public function index(): Response
{
    return $this->render('movies/index.html.twig');
}

} ?>

index.html.twig full code

{% extends "./base.html.twig" %}

{% block body %}

Movies Review ({{ movies|length }})

Created by Code With Dary
<div class="mx-auto w-4/5 my-8">
    <a 
        href="/movies/create" 
        class="uppercase border border-gray-500 text-lg py-4 px-6 rounded transition transition-all bg-gray-800 text-white hover:bg-white hover:text-gray-800">
        Create New movie
    </a>
</div>
<div class="md:grid lg:grid-cols-3 gap-20 w-4/5 mx-auto py-15 ">
    <!-- Review Item -->
    {% for movie in movies %}
        <div class="text-center pt-8 pb-4">
            <img
                src="{{ movie.imagePath }}"
                alt=""
                class="shadow-xl rounded-md"
            />

            <h2 class="text-gray-700 font-bold text-3xl py-2">
                {{ movie.title }}
            </h2>

            <span class="text-gray-500">
                By <span class="italic text-sm text-gray-800">Code With Dary | 28.01.2022
            </span>

            <p class="text-base text-gray-700 pt-4 pb-10 leading-8 font-light">
                {{ movie.description }}
            </p>

            <a href="/movies/{{ movie.id }}" class="uppercase border border-gray-500 text-gray-600 text-lg py-4 px-12 rounded transition transition-all hover:bg-gray-800 hover:text-white">
                Keep Reading
            </a>
        </div>
    {% endfor %}
</div>

{% endblock %}

base.html.twig becase we extended class

<!DOCTYPE html>

{% block title %}Welcome!{% endblock %} {% block stylesheets %}{% endblock %} {% block javascripts %}{{ encore_entry_script_tags('app') }}{% endblock %} {% block header %}
{% endblock %} {% block body %}{% endblock %} {% block footer %}

Copyright 2021 Code With Dary. All Rights Reserved

{% endblock %}
kiwiFella commented 1 year ago

i had the same problem - 3 hrs into the tutorial and thought i hit a road block...

However a quick search on symfony website (https://symfony.com/doc/current/templates.html#creating-templates) shows you need to pass the dynamic variables with the render function

So if you followed along with the tutorial - in your MoviesController file you would have created a $movies DB query - something like $movies = $repository->findAll()

You need to pass that along with the render method like this... return $this->render('movies/index.html.twig', ['movies' => $movies,]);

Seems strange that this is missing from the MoviesController file in git repo

Hope that helps.