Link | cache_assets |
Author | Jason Huck |
Category | Utility |
Version | 8.5.x |
License | Public Domain |
Posted | 22 Sep 2008 |
Updated | 22 Sep 2008 |
More by this author... |
Given an array of file paths for JavaScript or CSS files, this tag will concatenate all the files into one, run it through YUI Compressor for minification, optionally GZip compress it, write the result to disk in the specified location, and return an HTML <link> or <script> tag pointing to the file. The file is named with a checksum based on the files' paths and modification dates, and the tag will look for a matching file first before doing any further processing, essentially caching the results. Requires several other tags: [shell], [yui_compress], [url_normalize], [css_normalizeurls], and [compress_gzip].
var('mystyles') = array( '/styles/foo.css', '/styles/bar.css', '/styles/baz.css' ); cache_assets( $mystyles, -target='/styles/cache/', -usecache=true, -refresh=false, -minify=true, -compress=false ); -> ''
Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
define_tag( 'assets', -namespace='cache_', -req='in', -type='array', -opt='target', -type='string', -opt='usecache', -type='boolean', -opt='refresh', -type='boolean', -opt='minify', -type='boolean', -opt='compress', -type='boolean', -priority='replace', -encodenone, -description='Concatenates, minifies, and compresses JS and CSS files and returns a link to the resulting file(s).' ); local('srcfiles') = #in; local( 'out' = string, 'cacheid' = string, 'cachepath' = string, 'tempdata' = string, 'cachetype' = #srcfiles->first->split('.')->last ); local('defaults') = array( 'target' = '/site/' + (#cachetype == 'js' ? 'scripts' | 'styles') + '/cache/', 'usecache' = true, 'refresh' = false, 'minify' = true, 'compress' = true ); iterate(#defaults, local('i')); !local_defined(#i->first) ? local(#i->first) = #i->second; /iterate; // if caching is on if(#usecache); // generate unique path for the cached file per the given list of source files local('token') = string; iterate(#srcfiles, local('i')); local('d') = file_moddate(#i); #token += #i + #d; /iterate; #cacheid = encrypt_hmac( -token=#token, -password=server_name, -digest='md5', -cram ); !#target->endswith('/') ? #target += '/'; #cachepath = #target + #cacheid + '.' + #cachetype; // if refresh command is given or cached file is not found if(#refresh || !file_exists(#cachepath)); // concatenate source files into cached file iterate(#srcfiles, local('i')); local('src') = (#cachetype == 'css' ? css_normalizeurls(#i) | string(include_raw(#i))); #tempdata += #src + '\n\n'; /iterate; // if using minification if(#minify); // compress/pack/minify cached file (YUI can handle both js and css) file_write(#cachepath, #tempdata, -fileoverwrite); #tempdata = yui_compress(server_webroot + #cachepath); #cachepath->replace('.' + #cachetype,'-min.' + #cachetype); /if; if(#compress); // gzip result #tempdata = compress_gzip(#tempdata); #cachepath += '.gz'; /if; // write cached file to disk file_write(#cachepath, #tempdata, -fileoverwrite); /if; // make sure cachepath is correct even when not refreshing #minify && #cachepath !>> '-min' ? #cachepath->replace('.' + #cachetype,'-min.' + #cachetype); #compress && !#cachepath->endswith('.gz') ? #cachepath += '.gz'; // generate link to cached file if(#cachetype == 'js'); #out += '\n'; else; #out += '\n'; /if; // else just generate separate links for the individual source files else; iterate(#srcfiles, local('i')); if(#cachetype == 'js'); #out += '\n'; else(#cachetype == 'css'); #out += '\n'; /if; /iterate; /if; return(#out); /define_tag;
No comments
©LassoSoft Inc 2015 | Web Development by Treefrog Inc | Privacy | Legal terms and Shipping | Contact LassoSoft