Every so often I find it necessary to overwrite certain styles or implement 
styles or js functionality specific to certain pages in my app. For some 
projects my app application-wide stylesheets and js can get really big 
really fast and I find it more manageable to break them down into smaller 
files and only call them when needed. I put together a really simple Asset 
Helper that replaces my Html->css and Html->script and incorporates the 
assets based on controller/actions. The comment was made recently that it 
was a really bad idea and would create issues as the app grows larger and 
traffic to the app increases. They didn't elaborate on the comment so now 
it leaves me wondering.

I can't see where it would create any issues to speak of. My css and js 
directories contain a fair amount of files but is still extremely small by 
comparison so I don't really see any type of significant performance hit. 
Am I overlooking something here?

// AssetHelper.php

App::uses('AppHelper', 'View/Helper');
App::uses('Folder', 'Utility');
    
    Class AssetHelper extends AppHelper{
        
        public $helpers = array('Html');
        
        public function css($css_array = null){
            
            $css = new Folder(WWW_ROOT . 'css');
            $css_files = $css->find('.*\.css');
            
            if ($this->params['controller'] == 'pages'){
                $file = "pages_{$this->params['pass'][0]}";
            } else {
                $file = 
$this->params['controller'].'_'.$this->params['action']; 
            }
            
            if (in_array($file.'.css', $css_files)){
                array_push($css_array, $file);
            }
            
            return $this->Html->css($css_array);
        }
        
        public function js($js_array = null){
            
            $js = new Folder(WWW_ROOT . 'js');
            $js_files = $js->find('.*\.js');    
            
            if ($this->params['controller'] == 'pages'){
                $file = "pages_{$this->params['pass'][0]}";
            } else {
                $file = 
$this->params['controller'].'_'.$this->params['action']; 
            }
            
            if (in_array($file.'.js', $js_files)){
                array_push($js_array, $file);
            }
            
            return $this->Html->script($js_array);
        }
        
    }

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cake-php+unsubscr...@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to