Schmilblick
Schmilblick

Reputation: 21

[Symfony 3][Sonata Admin] setTemplate problem

I encounter a problem when I want to create a custom template for the creation and editing view of my entity.

Unable to find template "AppBundle:Admin:Estate:edit.html.twig" (looked into: C:\wamp64\www\ibg\vendor\knplabs\knp-menu\src\Knp\Menu/Resources/views, C:\wamp64\www\ibg\vendor\symfony\symfony\src\Symfony\Bridge\Twig/Resources/views/Form).

Services.yml

app.admin.estate:
    class: AppBundle\Admin\EstateAdmin
    tags:
        - { name: sonata.admin, manager_type: orm, label: "Biens immobiliers" }
    arguments:
        - ~
        - AppBundle\Entity\Estate
        - ~
    calls:
         - [ setTemplate, [edit, AppBundle:Admin/Estate/edit.html.twig]]
    public: true

I tested lot's of syntaxe for setTemplate but always the same message

- [ setTemplate, [edit, AppBundle:Admin:Estate:edit.html.twig]]
- [ setTemplate, [edit, AppBundle:Admin/Estate:edit.html.twig]]
- [ setTemplate, [edit, AppBundle/Resources/views/Admin/Estate/edit.html.twig]]

or

The service "app.admin.estate.template_registry" has a dependency on a non-existent service "App/Admin/Estate:edit.html.twig".

When I use

- [ setTemplate, [edit,"@App/Resources/views/Admin/Estate/edit.html.twig"]]
- [ setTemplate, [edit,"@App/Admin/Estate/edit.html.twig"]]
- [ setTemplate, [edit,"@App\Admin\Estate\edit.html.twig"]]

The path of my twig file is src\AppBundle\Resources\views\Admin\Estate\edit.html.twig

I don't understand ... I have an old project with sonata and symfony and it work well

- [ setTemplate, [edit, AppBundle:Admin/Exemple:exemple.html.twig]]

Sonata admin version 3.40.2

Symfony 3.4.17

Thank you for your help.

Upvotes: 2

Views: 555

Answers (1)

Endre B.
Endre B.

Reputation: 101

After bashing my head against this one for a bit, I discovered that the solution (hack?) is to prefix the twig name with another @ symbol. E.g.:

- [ setTemplate, [edit,"@@App/Admin/Estate/edit.html.twig"]]

That prevents the "non-existent service" error and gets the namespace (App) recognised correctly in Twig_Loader_Filesystem when it's resolving the twig location. I can't find any official documentation to explain this as an official thing, but it works for now.

For future searchers, this is working with Symfony 3.4.18, Sonata Admin 3.40.3, and Twig 2.5.0.

Upvotes: 3

Related Questions