简体   繁体   中英

Symfony 4 - Unable to find template

I updated my project to Symfony4. When I go to some page with a form I have this error appearing :

Unable to find template "StarRatingBundle::rating.html.twig" 
(looked into: /templates, /templates, \vendor\symfony\twig-bridge/Resources/views/Form).

I use blackknight467/star-rating-bundle a bundle which provides a twig extension to have a star rating system. The template in the error is located in this bundle Resources/views folder.

Twig.yaml :

twig:
    paths: ['%kernel.project_dir%/templates']
    debug: '%kernel.debug%'
    strict_variables: '%kernel.debug%'
    form_themes:
        - 'Form/fields.html.twig'

Symfony 4+ does not support “x:x:x.html.twig” notation. You need to install composer require templating

then activate it in your framework.yaml by adding

templating: engines: ['twig'] .

Files > Invalidate caches & restart. This should allow Symfony to find your templates.

I struggled with the issue when creating a symfony 5 bundle. Make sure you keep your twig templates in <project-folder>/Resources/views folder. In your Controller, using HelloworldController as an example:

`<?php
    namespace Example\HelloBundle\Controller;
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\Routing\Annotation\Route;

    class HelloController extends AbstractController
    {
        public function index(): Response
        {
            return $this->render('@<BundleName>/hello/index.html.twig',           [
            'controller_name' => 'HelloController',
            ]);
        }
    }

is the name of your bundle without the bundle suffix. For example if you registered your bundle as Example\\HelloBundle\\ExampleHelloBundle::class => ['all' => true], in Bundles.php then <BundleName> is ExampleHello

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM