Random blurps and rants about programming
RSS icon Email icon Home icon
  • Symfony: selectively replacing application templates with a plugin

    Posted on February 15th, 2009 klti 3 comments

    After many headaches about how the hell I can get symfony to look for a layout or a template in a plugin directory first, so that you can selectively replace default layouts (meaning the ones in the application or module template directory) with versions provided by a specific plugin, I figured out a simple and yet elegant solution. It actually took me a few hours of crawling through symfony code to figure out that it all boils down to the application configuration.

    Solution: Simply overwrite the directoy getter in the Configuration class of your application and insert your disired location at the front of the array.

    class MyExampleApplicationConfiguration extends sfApplicationConfiguration
    {
    [..]
    public function getTemplateDirs($moduleName)
    {
    $dirs = parent::getTemplateDirs($moduleName);
    array_unshift($dirs,sfConfig::get('sf_plugins_dir').'/SomePlugin/apps/'.$this->getApplication().'/modules/'.$moduleName.'/templates');
    return $dirs;
    }
    [...]
    }

    Do the same thing with getDecoratorDirs() to replace layout files. It even works for I18n, because they too use a function in sfApplicationConfiguration to get their candidate directories.

     

    3 responses to “Symfony: selectively replacing application templates with a plugin”

    1. Awesome Solution! it was exactly what I was looking for. Kudos

    2. great work! i’ve been looking for a way to reskin my app based on the virtual host for a while. i think this should just about do the job.

    3. Great !

    Leave a reply

    :D :-) :( :o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: