Symfony2 中使用 Twig 的动态主题

发布于 2024-11-25 14:13:53 字数 717 浏览 2 评论 0原文

我正在为 Symfony2 设计一个多租户应用程序,其中每个租户都可以有一个覆盖默认应用程序模板的主题。因此,主题将具有唯一的 base.html.twig 文件,并且可能包含也可能不包含覆盖默认模板文件的其他文件。

Symfony2 已经检查 app/Resources/views 是否有覆盖捆绑包模板的模板。但 Symfony2 假设 app/Resources/views 只有一组可以覆盖默认模板的模板。我想动态检查租户的自定义主题文件夹中的各种覆盖模板,例如:

  1. 主题:
    • app/Resources/views/theme1/base.html.twig
  2. 主题:
    • 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.:

  1. Theme:
    • app/Resources/views/theme1/base.html.twig
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迟到的我 2024-12-02 14:13:53

我在尝试做这样的事情时遇到了困难...我查看了 liipthemebundle 的代码,它需要很多配置...我在互联网上查找了很多...然后我开始思考...并且我看到的是这样的:

http://symfony.com/doc/current/book/templatating.html#overriding-bundle-templates

该页面中有很多有用的信息......但是是什么让我一个简单的解决方案是这样的事实:symfony 在 app/Resources/[MyBundle] 中查找模板和东西......我发现了
负责此操作的服务是 file_locator 服务...

所以,如果您定义了一个参数,可以说在parameters.yml 中设置皮肤

,并将此行添加到您的 app/config/config.yml 中,

file_locator:
        class: %file_locator.class%
        arguments: [@kernel,%kernel.root_dir%/Resources/skins/%skin%]

您就拥有了自己的皮肤...

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

file_locator:
        class: %file_locator.class%
        arguments: [@kernel,%kernel.root_dir%/Resources/skins/%skin%]

you have yours skins...

孤芳又自赏 2024-12-02 14:13:53

我遇到了同样的问题,我使用 LiipThemeBundle 来解决它。我花了几分钟来配置:

  • 使用 Composer 安装捆绑包,然后激活它。
  • 配置捆绑包 (app/config/config.yml)


    liip_theme:
        themes: ['theme1', 'theme2', 'theme3']
        active_theme: 'theme1'

  • 将三行复制到 app/config/routing.yml


    liip_theme:
        resource: "@LiipThemeBundle/Resources/config/routing.xml"
        prefix: /theme

  • 将文件从 Resources\views\ 移动到 Resources\themes\theme1\

并准备就绪!

之后,当我在控制器中渲染模板时:



    return $this->render('AcmeDemoBundle:Demo:index.html.twig');

它使用位于“Resources\themes\theme1\Demo\index.html.twig”中的文件。当我需要切换到另一个主题时,就我而言,因为我的模型的某些实体具有自定义主题,我可以用一行代码来完成:

 

    $this->get('liip_theme.active_theme')->setName('theme2');
    return $this->render('AcmeDemoBundle: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:

  • install the bundle with composer, and activate it.
  • config the bundle (app/config/config.yml)


    liip_theme:
        themes: ['theme1', 'theme2', 'theme3']
        active_theme: 'theme1'

  • copy three lines to app/config/routing.yml


    liip_theme:
        resource: "@LiipThemeBundle/Resources/config/routing.xml"
        prefix: /theme

  • move the files from Resources\views\ to Resources\themes\theme1\

and ready !!

after that, when i render a template in the controller:



    return $this->render('AcmeDemoBundle:Demo:index.html.twig');

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:

 

    $this->get('liip_theme.active_theme')->setName('theme2');
    return $this->render('AcmeDemoBundle:Demo:index.html.twig');

and now uses the file located in "Resources\themes\theme2\Demo\index.html.twig"

that easy !! (and clean)

沦落红尘 2024-12-02 14:13:53

了解捆绑包: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文