Introduction

While developing single Symfony2 application on multiple domains for example e-commerce with multiple stores that are using same controller actions or any other common usage, you will probably need to change template folder according to domain or sub-domain.

In our example

we consider 2 websites on different domains that represents two websites

site1.com
site2.com

How Twig Loader works

Most of the applications works with single Twig Loader (default) where all template folders and names spaces are registered and available to be loaded by your controllers, by default all the project bundles are registered automatically in your Twig loader and accessible thought direct access with physical path

$this->render('path/to/my/twig');

or by namespace (commonly used in Symfony2)

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

or

$this->render('@AcmeDemo/Demo/index.html.twig');

in order to achieve using different templating (themes) folders you will have two options :

To change the template directory paths of current Twig Loader by adding new directory paths.

Create new custom Twig Loader to work along with the existing one, that has your custom directory paths and structure.

We will go through the first option is to add paths of the current TwigLoader

Configuration:

We will have to set some configuration for the the two stores

if you are not familiar with custom configuration you can check Symfony2 docs here
in /src/Acme/DemoBundle/Resources/config/config.yml

Ahmed Samy Post author

Also, I don’t know if it matters, but I bypassed all the special config for hard coded values because I needed to do this dynamically based on the current site. So I just set the alternative path directly in my controller (after I know which template the current site is using):

Ahmed Samy Post author

Hey Chatwick, generally i am not sure you are heading to the right solution for your specific case
how much code is shared by these different websites ?
if it’s a lot of code you may consider bundle inheritance instead, it will cover template overriding just as normal Symfony2 way and it will allow you to use assets without any hacks and same as any other parts too

so you can have different apps (sites) every app has several bundles

Finally this approach really helps when you have multiple websites that are using same widgets and pages, but it’s not good to work along side with bundle inheritance

I am building a hosted CMS platform that will have a lot of different sites as well, each will either use one of our templates or even a custom template that will need to be stored outside the symfony project root in their own vhost folder… so thanks for these ideas.

1. Where do you store the assets (Resources/public)? Do you put in a similar path `Resources/public/Site1` so that assets:install –symlinks will link to them as well? I had trouble with assets when I specified an alternative template directory.See this question and this one too.

2. I ended up creating a special `Templates` vendor with different theme bundles, and then each site has a setting which specifies which template to use. This lets me overwrite core controllers as well as templates. But I don’t yet have a solution for the assets in custom client vhost paths.See this question.

The problem that I still face is the need to override the templates of other bundles (e.g. a template may need a special way to display the ArticleBundle). Any ideas would be much appreciated.

Fony

Vincent Bergeron

I followed the instructions and it`s not working for me.

I’m using the latest version of Symfony. The views are all loaded from the default folder (Resources/views).

I cleared the cache, putted a die(`here`); in the constructor of the TemplateListener. Then I looked in the appDevDebugProjectContainer.php, in the getTwig_LoaderService and the default views folder is listed there.

So this file is being constructed before the TemplateListener gets called. I think that`s why my addPath and prependPath is ignored.

Maybe there`s something different with the version I`m using… I would really like to use this method for my theming need.

Guillermo

Ahmed Samy Post author

if you have more than one bundle with view files it should work normally, as this listener adds paths to look at directly but does not remove any , so other bundles views loadable normally with their namespaces or direct path

Pepo

Actually this is interesting
I want to have more domains deployed inside a single Symfony installation but I want for each domain a different bundle which will be handling it (routes, views, models etc) – is this possible, or I understand Symfony a bit weirdly?
Is it a good idea to actually use a single instance of Symfony for more different “sites” (I’m thinking single sign on as an example)

Ahmed Samy Post author

It really depends on the business model, how much common features shared between different sites , but this post mainly resolve the problem with different templates (twig files) according to domain or sub-domain

but at some point you may need to use different app inside the project