在 Symfony 框架中保存 TWIG 模板的位置

Where to save your TWIG templates in Symfony framework

保存 TWIG 模板的最佳位置是什么?为什么?

app/Resources/views folder

YourBundle/Resources/views 

开发应用程序时,最好的地方是YourBundle/Resources/views。它将更加一致,并且可能更易于维护,并最终解耦您的捆绑包以安装在其他项目中。

我发现 app/Resources/views 对覆盖其他包的模板很有用,例如在创建错误页面时 http://symfony.com/doc/current/cookbook/controller/error_pages.html

Traditionally, Symfony developers stored the application templates in the Resources/views/ directory of each bundle. Then they used the logical name to refer to them (e.g. AcmeDemoBundle:Default:index.html.twig).

But for the templates used in your application, it's much more convenient to store them in the app/Resources/views/ directory. Syfmony Documentation

  • 在 Symfony 2 中,自动生成的 twig 文件位于 YourBundle/Resources/views
  • 在 Symfony 3 中,自动生成的 twig 文件位于 app/Resources/views

这取决于您的捆绑包架构。重点是与您当前的架构保持一致。 来自官方文档:

For most projects, you should store everything inside the AppBundle

如果您使用主捆绑包结构(如带有许多小域捆绑包的大型 AppBundle),那么您可能希望将您的模板集中在应用程序或主捆绑包的资源中。 但是,如果您使用功能包(如 EmailBundle、UserBundle、Invoice Bundle),那么您可能想忽略 symfony 最佳实践并将相关的 twig 放入这些包中。