找不到 Symfony/Sonata 模板路径文件以及目录结构的排列方式 [英] Symfony/Sonata template path file not found and how directory structures are arranged
问题描述
我使用的是 Symfony v3.3.13 和 Sonata Admin Bundle 3.27.0 和 TWIG v2.4.4
I am using Symfony v3.3.13 and Sonata Admin Bundle 3.27.0 with TWIG v2.4.4
我在理解树枝模板路径的工作原理时遇到了一些问题.我最近需要为 SonataAdminBundle
中的列表视图字段加载一个新模板.
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
.
我必须如下定义路径:
->add('coords', null, ['template' => 'SonataAdmin/CRUD/geography_point_list.html.twig']);
geography_point_list.html.twig 文件位于 app/Resources/views/SonatAdmin/Crud
The geography_point_list.html.twig file was in app/Resources/views/SonatAdmin/Crud
我还设法通过路径 AppBundle::SonataAdmin/CRUD/geography_point_list.html.twig
完成了这项工作.但是,该文件应该在 src/AppBundle/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
但是我在所有示例中看到模板文件路径被冒号 :
而不是斜线 /
分隔,但我无法得到类似 AppBundle:SonataAdmin 的东西:CRUD:geography_point_list.html.twig
工作.我尝试了几种变体,但总是出现 无法找到模板
.以下都不起作用(我在 app/Resources/views/SonataAdmin/CRUD
和 src/AppBundle/Resources/views/SonataAdmin/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- 为什么它不起作用?我究竟做错了什么?或者可能缺少什么?因为我可以看到在 SonataAdminBundle
中定义了像 SonataAdminBundle:CRUD:list.html.twig
这样的路径,并且它们似乎正在工作?
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- 在 模板命名和位置文档 它说放置模板的正确位置是 app/Resources/views
2- In template naming and locations documentation it says the correct place to put the templates is app/Resources/views
a) 在 AppBundle
中存储模板是否已过时?b) 为什么将模板存储在 AppBundle
目录中没有意义?我不明白的是为什么 app
目录有 config
和 Resources
与 AppBundle
分开?
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
?
推荐答案
我认为它不起作用,因为您应该尝试
BundleName:DirectoryInViewsFolder:Subfolder/template.html.twig
.因此,在您的情况下,它是AppBundle:SonataAdmin:CRUD/geography_point_list.html.twig
.
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) 在 symfony 3 中我不这么认为.
a) In symfony 3 i don't think so.
b) 我猜在 app/Resources
和 app/config
中有你的应用程序的全局资源和配置.以及您放在 src/BundleName/Resources
和 src/BundleName/config
文件夹中的包的资源和配置.
b) I guess what in app/Resources
and app/config
there are global resources and configs for your application. And resources and configs for your bundle you put in src/BundleName/Resources
and src/BundleName/config
folders.
顺便说一句,您也可以使用您的自定义模板路径:
BTW you can also use your custom templates paths:
config.yml
twig:
paths:
'%kernel.project_dir%/src/YourBundle/templates': templates
您的管理员.php
->add('coords', null, ['template' => '@templates/geography_point_list.html.twig']);
更新
这个答案并不完全正确.这对 Evren Yurtesen 很有帮助,但您应该在此处看到正确答案.
This answer is not fully correct. It was helpful for Evren Yurtesen but you should see the right answer here.
这篇关于找不到 Symfony/Sonata 模板路径文件以及目录结构的排列方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!