Sunday, August 19, 2012

Simple script to auto load css/js from webroot - CakePhp

This is very simple script to auto-load all css/js files from the default webroot/css, webroot/js folders.

Script isnt flexible and feel free to build on it and use it in your apps

App::uses('Folder', 'Utility');
App::uses('File', 'Utility');

class AutoloadHelper extends AppHelper {
   
    var $helpers = array('Javascript', 'Html');
    
    function js()
    {
     $this->walker('js');
    }
    
    function css()
    {
     $this->walker('css');
    }


    private function walker( $subpath = "")
    {
     $path = WWW_ROOT .$subpath;
     
     $dir = new Folder($path);
     $files = $dir->find('.*\.'. $subpath);
     
     
     foreach($files as $file)
     {
      if($subpath == 'css')
      {
       echo $this->Html->css($file);
      }else
      {
       echo $this->Html->script($file);
      }
     }
    }
}



in your layout
Autoload->css();?>
Autoload->js();?>

No comments: