Code snippets for symfony 1.x


Add Style Sheet automaticaly from view class

This system will allow the inclusion of css automaticaly. You must create a folder structure in the web/css folder matching one or more of these naming patern.




class myPHPView extends sfPHPView
   * Executes any presentation logic for this view.
  public function execute()
  public function addDefaultStylesheet()
      $response = $this->getContext()->getResponse();
      $sf_web_dir = sfConfig::get('sf_web_dir') . '/css/';
      $module_file = $this->moduleName . '.css';
      $main_module_file =  $this->moduleName . '/main.css';
      $action_file = $this->moduleName . '/' . $this->actionName . '.css';
      if( is_readable( $sf_web_dir . $module_file ) ){
      if( is_readable( $sf_web_dir . $main_module_file ) ){
      if( is_readable( $sf_web_dir . $action_file ) ){

To change this you also need to change the module.yml and put this

  view_class:       myPHP
by Martin Poirier Theoret on 2008-02-27, tagged stylesheet  view 

Comments on this snippet

gravatar icon
#1 Dipak patel on 2008-02-29 at 08:10

Nice work !!. Thanks . I was looking for same.

gravatar icon
#2 Daniel Lohse on 2008-10-20 at 02:00

Wouldn't it be better to just put this into a filter? There are a few other view classes in the snippet section and what if you need the sfCulturePHPView (found here:

A filter which basically does this, can be found here:

Just my 2 cents.