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

我在理解树枝模板路径的工作原理时遇到了一点问题。我最近需要为 SonataAdminBundle.

中的列表视图字段加载新模板

我必须按如下方式定义路径:

->add('coords', null, ['template' => 'SonataAdmin/CRUD/geography_point_list.html.twig']);

geography_point_list.html.twig 文件位于 app/Resources/views/SonatAdmin/Crud

我还设法通过路径 AppBundle::SonataAdmin/CRUD/geography_point_list.html.twig 完成了这项工作。但是文件应该在 src/AppBundle/Resources/views/SonatAdmin/Crud

但是我在所有示例中都看到模板文件路径被冒号 : 而不是斜线 / 分隔,但我无法使用 AppBundle:SonataAdmin:CRUD:geography_point_list.html.twig 之类的东西。我尝试了几种变体,但总是得到 Unable to find template。 None 以下的工作(我在 app/Resources/views/SonataAdmin/CRUDsrc/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']);

好的。所以我有两个问题:

1- 为什么它不起作用?我究竟做错了什么?或者可能缺少什么?因为我可以看到像 SonataAdminBundle:CRUD:list.html.twig 这样的路径是在 SonataAdminBundle 中定义的,而且它们似乎在工作?

2- 在 template naming and locations documentation 中说放置模板的正确位置是 app/Resources/views

a) 在 AppBundle 中存储模板是否已弃用? b) 为什么将模板存储在 AppBundle 目录中没有意义?我不明白的是为什么 app 目录有 configResourcesAppBundle?

分开
  1. 我认为它不起作用,因为您应该尝试 BundleName:DirectoryInViewsFolder:Subfolder/template.html.twig。所以在你的情况下是 AppBundle:SonataAdmin:CRUD/geography_point_list.html.twig.

  2. 好的,你可以把它们放在那里。

    a) 在 symfony 3 中我不这么认为。

    b) 我猜 app/Resourcesapp/config 中有适用于您的应用程序的全局资源和配置。以及您放入 src/BundleName/Resourcessrc/BundleName/config 文件夹中的捆绑包的资源和配置。

顺便说一句,您也可以使用 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']);

更新

这个答案不完全正确。这对 Evren Yurtesen 很有帮助,但您应该会看到正确的答案