Code snippets for symfony 1.x

Navigation

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.

web/css/[module_name].css

web/css/[module_name]/main.css

web/css/[module_name]/[action_name].css

class myPHPView extends sfPHPView
{
  /**
   * Executes any presentation logic for this view.
   */
  public function execute()
  {
    $this->addDefaultStylesheet();
  }
 
  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 ) ){
         $response->addStylesheet($module_file);
      }
      if( is_readable( $sf_web_dir . $main_module_file ) ){
         $response->addStylesheet($main_module_file);
      }
      if( is_readable( $sf_web_dir . $action_file ) ){
         $response->addStylesheet($action_file);
      }
  }
}
 

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

all:
  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: http://www.symfony-project.org/snippets/snippet/311)?

A filter which basically does this, can be found here: http://www.symfony-project.org/snippets/snippet/306

Just my 2 cents.