1

I did a symfony migration from version 2.7 to version 3.4

Every thing is ok except one thing. The twig files are not working the error message is :

Unable to find template "::layout.html.twig" (looked into: vendor\symfony\symfony\src\Symfony\Bridge\Twig/Resources/views/Form, vendor\knplabs\knp-menu\src\Knp\Menu/Resources/views) in abcdBundle::layout.html.twig at line 3.

The twig code is :

{% extends "::layout.html.twig" %}

I want to load the layout localised into app/Resources/views/layout.thml.twig. I have tried this code too : {% extends "layout.html.twig" %} without :: same troubles.

Location files:

app/
    Resources/
        views/
            layout.html.twig // it don't find this twig

src/
    ab/
        cdBundle/
            Ressources/
                views/
                    layout.html.twig // error into this file
R3tep
  • 12,512
  • 10
  • 48
  • 75
  • Colon based template paths are no longer supported by default. https://stackoverflow.com/questions/47832977/symfony-3-4-use-view-inside-my-bundle/47835716#47835716 – Cerad Dec 04 '18 at 13:53
  • @Cerad Ok, but how to solve my issue ? I have already seen this post. But I did not understand how to make my twig work again. – R3tep Dec 04 '18 at 13:59
  • Use twig namespaces (preferred solutions) or add the twig engine to your templating config as shown at the very end of the admittedly lengthy answer. I am assuming that this all worked under your 2.7 app and there are no typos etc. – Cerad Dec 04 '18 at 14:04
  • @Cerad I have already added the templating config. All working yes before the migration. I tried this code: `{% extends "@App/layout.html.twig "%}` (with namespace), I have this error `There are no registered paths for namespace" App "`. – R3tep Dec 04 '18 at 14:09
  • 1
    For 3.4 I would expect @AppBundle/layout.html.twig. "bin/console debug:twig" will show you available namespaces. But just plain layout.html.twig really should work. And while I suspect it is just a question typo, Ressources is spelt wrong. I know it can be very confusing because there are several different directory structures supported for 3.4. – Cerad Dec 04 '18 at 14:21
  • @Cerad Thanks a lot, I found a solution with yours sources. – R3tep Dec 04 '18 at 14:37

2 Answers2

1

If you want to make your project more bulletproof and ready for future migrations to Symfony 4 – consider moving all your Resources away from app/ directory.

You can find more ie. in here:

http://fabien.potencier.org/symfony4-directory-structure.html

cadavre
  • 1,334
  • 1
  • 18
  • 34
0

I have updated the config.yml file to add :

twig:
    paths:
        '%kernel.project_dir%/app/Resources/views': app

This added the "Namespace" @app in the "Loader Paths" of bin/console debug:twig

Now, when I use {% extends "@app/layout.html.twig" %} its working fine.

Thank @Cerad for your help, it's very appreciated

R3tep
  • 12,512
  • 10
  • 48
  • 75
  • Glad you got it working. Just out of curiosity, does debug:twig show a "(None) namespace perhaps pointing to templates? – Cerad Dec 04 '18 at 14:44
  • @Cerad Yes ! At the bottom of the list, there is a "None" pointing to `vendor\symfony\symfony\src\Symfony\Bridge\Twig/Resources/views/Form\ `. Is this the origin of the problem? – R3tep Dec 04 '18 at 14:49
  • 1
    I think so. None is the default location for templates and under the new Symfony directory structure it points to project/templates. Don't know why it would point to a Twig bridge directory. Might try something like '%kernel.project_dir%/templates': '' and see if you can get the need for an app namespace to go away. It will also help if you ever upgrade to 4x. – Cerad Dec 04 '18 at 14:59
  • If this path `%kernel.root_dir%/Resources/views` really exists, it's auto-added to the Twig paths (without namespace `(None)`) and so `{% extends "layout.html.twig" %}` should work. – yceruto Dec 05 '18 at 14:22
  • And the Twig bridge directory is there (without namespace) because it's registered by TwigBundle to find/use/extends from any form theme layout (e.g. `form_div_layout.html.twig`) easily. – yceruto Dec 05 '18 at 14:25
  • About the missing path `%kernel.project_dir%/templates/`, it is required that the directory exists to be added to the Twig paths (it apply for all paths). – yceruto Dec 05 '18 at 14:30