问题
I am using Symfony v3.3.13 and Sonata Admin Bundle 3.27.0 with TWIG v2.4.4
I have a little problem understanding how twig template paths work. I have recently needed to load a new template for a list view field in SonataAdminBundle
.
I had to define the path as follows:
->add('coords', null, ['template' => 'SonataAdmin/CRUD/geography_point_list.html.twig']);
The geography_point_list.html.twig file was in app/Resources/views/SonatAdmin/Crud
I also managed to get this work with the path AppBundle::SonataAdmin/CRUD/geography_point_list.html.twig
. However then the file should be in src/AppBundle/Resources/views/SonatAdmin/Crud
However I see in all examples that the template file path is divided by colon :
instead of slash /
yet, I cant get something like AppBundle:SonataAdmin:CRUD:geography_point_list.html.twig
to work. I tried several variations and I always get Unable to find template
. None of the following worked (I have the in both app/Resources/views/SonataAdmin/CRUD
and src/AppBundle/Resources/views/SonataAdmin/Crud
:
->add('coords', null, ['template' => 'App:SonataAdmin:CRUD:geography_point_list.html.twig']);
->add('coords', null, ['template' => 'AppBundle:SonataAdmin:CRUD:geography_point_list.html.twig']);
->add('coords', null, ['template' => 'AppBundle:app:Resources:views:SonataAdmin:CRUD:geography_point_list.html.twig']);
->add('coords', null, ['template' => 'App::SonataAdmin:CRUD:geography_point_list.html.twig']);
->add('coords', null, ['template' => 'AppBundle::SonataAdmin:CRUD:geography_point_list.html.twig']);
OK. So I have 2 questions:
1- Why it doesn't work? What am I doing wrong? or what may be missing? Because I can see that paths like SonataAdminBundle:CRUD:list.html.twig
is defined in SonataAdminBundle
and they seem to be working?
2- In template naming and locations documentation it says the correct place to put the templates is app/Resources/views
a) Is storing templates in AppBundle
deprecated?
b) Why it doesnt make sense to store the templates in AppBundle
directory instead? What I don't understand is why app
directory has config
and Resources
separate from AppBundle
?
回答1:
I think it doesn't work because you should try
BundleName:DirectoryInViewsFolder:Subfolder/template.html.twig
. So in your case it isAppBundle:SonataAdmin:CRUD/geography_point_list.html.twig
.Right, you can put them there.
a) In symfony 3 i don't think so.
b) I guess what in
app/Resources
andapp/config
there are global resources and configs for your application. And resources and configs for your bundle you put insrc/BundleName/Resources
andsrc/BundleName/config
folders.
BTW you can also use your custom templates paths:
config.yml
twig:
paths:
'%kernel.project_dir%/src/YourBundle/templates': templates
YourAdmin.php
->add('coords', null, ['template' => '@templates/geography_point_list.html.twig']);
UPDATE
This answer is not fully correct. It was helpful for Evren Yurtesen but you should see the right answer here.
来源:https://stackoverflow.com/questions/47497251/symfony-sonata-template-path-file-not-found-and-how-directory-structures-are-arr