[cache_assets]

Description

Link: [cache_assets]
Author: Jason Huck
Category: Utility
Version: 8.5.x
License: Public Domain
Posted: Sep. 22, 2008
Updated: Jan. 01, 0001
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].

Parameters

-target string, optional The path to the directory where the resulting file should be written.
-usecache boolean, optional Whether to combine and process the files (true, default) or just return links to each of the individual files.
-refresh boolean, optional If true, forces the files to be processed even if a matching cached file is found. Defaults to false.
-minify boolean, optional Whether to run the combined file through YUI Compressor. Defaults to true.
-compress boolean, optional Whether to GZip compress the combined file. Defaults to false.

Sample Usage

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
);


-> '<link rel="stylesheet" type="text/css" href="/styles/cache/(checksumvalue)-min.css" />'
						

Source Code

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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 += '<script type="text/javascript" src="' + #cachepath + '"></script>\n';
		else;
			#out += '<link rel="stylesheet" type="text/css" href="' + #cachepath + '" />\n';
		/if;
	
	// else just generate separate links for the individual source files
	else;	
		iterate(#srcfiles, local('i'));
			if(#cachetype == 'js');
				#out += '<script type="text/javascript" src="' + #i + '"></script>\n';
			else(#cachetype == 'css');
				#out += '<link rel="stylesheet" type="text/css" href="' + #i + '" />\n';
			/if;
		/iterate;
	/if;
	
	return(#out);
/define_tag;

 

Related Tags



Comments

none

Email:


Password:



Newest

Most Popular