2006-03-06

Dynamic template_root

I am currently developing a multi-site application. One of the features is that it picks layouts and page templates basing on the domain. I was trying to put everything domain-related in a separate directory like this:


RAILS_ROOT
 \- sites
    |- domain_a
    |  |- layouts
    |  |- templates
    |  \- stylesheets
    |
    \- domain_b

My first guess was to set the @@template_root of the controller from within before_filter. It didn't worked as desired.

Second attempt was to actually override the method self.template_root in the controller. It seemed to work fine - but only for default site.

Now the real problem is that Controller.template_root is read only once, when the controller is instantiated. In fact it is read before it can be set to something meaningful in before_filter.

I have solved this problem by using render :file => path and not relying on template_root at all. Another possible solution might be to modify the @@template.base_path from your controller, but I have not looked at this possibility yet.

0 comments: