Friday, January 10, 2014

Re: Assets Helper

I never thought to look because app assets are precompiled/compressed during development, the helper was just a quick and simple solution for the time being. I'll be sure to clone a copy and have a look, thanks Mark.


On Fri, Jan 10, 2014 at 5:48 AM, euromark <dereuromark@gmail.com> wrote:
Whats wrong with using the state of the art tool here, that is designed to handle assets?
https://github.com/markstory/asset_compress

Am Donnerstag, 9. Januar 2014 23:05:45 UTC+1 schrieb CrotchFrog:
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 a topic in the Google Groups "CakePHP" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cake-php/nT6OrowpejQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cake-php+unsubscribe@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.

--
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+unsubscribe@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.

No comments: