Symfony2 中使用 Twig 的动态主题
我正在为 Symfony2 设计一个多租户应用程序,其中每个租户都可以有一个覆盖默认应用程序模板的主题。因此,主题将具有唯一的 base.html.twig
文件,并且可能包含也可能不包含覆盖默认模板文件的其他文件。
Symfony2 已经检查 app/Resources/views
是否有覆盖捆绑包模板的模板。但 Symfony2 假设 app/Resources/views 只有一组可以覆盖默认模板的模板。我想动态检查租户的自定义主题文件夹中的各种覆盖模板,例如:
- 主题:
app/Resources/views/theme1/base.html.twig
- 主题:
app/Resources/views/theme2/base.html.twig
app/Resources/views/theme2/SomeBundle/Resources/views/page.html.twig
我不确定在 Symfony2 中构造它并在 Twig 中配置它的最佳方法。我应该将所有不同的主题堆放在应用程序/资源/视图中的文件夹中吗?或者我应该创建某种处理一切的 ThemeBundle ?谢谢!
I'm designing a multi-tenant application for Symfony2, where each tenant can have a theme that overrides the default application templates. So a theme will have a unique base.html.twig
file, and may or may not include other files that override the default template files.
Symfony2 already checks app/Resources/views
for templates that override the bundle templates. But Symfony2 assumes app/Resources/views
has just one set of templates that can override the default templates. I want to dynamically check a tenant's custom theme folder for various overriding templates, e.g.:
- Theme:
app/Resources/views/theme1/base.html.twig
- Theme:
app/Resources/views/theme2/base.html.twig
app/Resources/views/theme2/SomeBundle/Resources/views/page.html.twig
I'm not sure the best way to structure this in Symfony2 and to configure it in Twig. Should I pile all of the different themes into folders in app/Resources/views? Or should I create some kind of ThemeBundle that handles everything? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在尝试做这样的事情时遇到了困难...我查看了 liipthemebundle 的代码,它需要很多配置...我在互联网上查找了很多...然后我开始思考...并且我看到的是这样的:
http://symfony.com/doc/current/book/templatating.html#overriding-bundle-templates
该页面中有很多有用的信息......但是是什么让我一个简单的解决方案是这样的事实:symfony 在 app/Resources/[MyBundle] 中查找模板和东西......我发现了
负责此操作的服务是 file_locator 服务...
所以,如果您定义了一个参数,可以说在parameters.yml 中设置皮肤
,并将此行添加到您的 app/config/config.yml 中,
您就拥有了自己的皮肤...
i have a bad time trying to do something like this... i i looked at the code of liipthemebundle and it need to much configuration... i looked over the internet a lot... and then i started to think... and what i saw was this:
http://symfony.com/doc/current/book/templating.html#overriding-bundle-templates
there a lot of usefull info in that page... but what took me to a simple solution was this fact: symfony look in app/Resources/[MyBundle] for templates and things... and i found out
that the service responsible for that is this the file_locator service...
so, if you define a parameter, lets say skin in parameters.yml
and add this lines to your app/config/config.yml
you have yours skins...
我遇到了同样的问题,我使用 LiipThemeBundle 来解决它。我花了几分钟来配置:
并准备就绪!
之后,当我在控制器中渲染模板时:
它使用位于“Resources\themes\theme1\Demo\index.html.twig”中的文件。当我需要切换到另一个主题时,就我而言,因为我的模型的某些实体具有自定义主题,我可以用一行代码来完成:
现在使用位于“Resources\themes\theme2<”中的文件/b>\Demo\index.html.twig"
就这么简单! (而且干净)
i had the same problem, and i used LiipThemeBundle to solve it. it took me a few minutes to configure:
and ready !!
after that, when i render a template in the controller:
it uses the file located in "Resources\themes\theme1\Demo\index.html.twig". when i need to switch to another theme, in my case, because some entities of my model have custom themes, i can do it with one line of code:
and now uses the file located in "Resources\themes\theme2\Demo\index.html.twig"
that easy !! (and clean)
了解捆绑包: Symfony2 捆绑包结构,一个用例 - 捆绑包支持主题。
了解主题解析和主题解析cascade: LiipThemeBundle - 附带可供阅读的代码,应该有您想要的一切。
Learn about bundles: Symfony2 Bundle Structure, a use case - Bundles support themes.
Learn about theme resolution & cascade: LiipThemeBundle - comes with code to read and should have everything you're looking for.