Code snippets for symfony 1.x

Navigation

Snippets by user Martin Poirier Theoret

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 
(2 comments)