Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic themes in Symfony2 using Twig

Tags:

php

twig

symfony

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!

like image 746
Acyra Avatar asked Jul 24 '11 12:07

Acyra


1 Answers

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...

like image 98
Javier Neyra Avatar answered Oct 17 '22 10:10

Javier Neyra