[asset_manager]

Description

Link: [asset_manager]
Author: Jason Huck
Category: Utility
Version: 8.5.x
License: Public Domain
Posted: Sep. 22, 2008
Updated: Sep. 24, 2008
More by this author...

This custom type is a wrapper for a set of tags which help you automate the optimization, compression, and inclusion of JavaScript and CSS files in your pages. See the sample code for a simple "Hello World" example. This blog post has more in-depth information.

Parameters

none


Sample Usage

[//lasso
	// must be authorized for file tags and os_process
	auth_admin;

	// make sure shell.lasso is loaded from lassostartup

	// load required tags
	library('tags/array_unique.inc');
	library('tags/asset_manager.inc');
	library('tags/cache_assets.inc');
	library('tags/css_normalizeurls.inc');
	library('tags/compress_gzip.inc');
	library('tags/server_webroot.inc');
	library('tags/url_normalize.inc');
	library('tags/yui_compress.inc');

	// initialize the asset manager	
	asset_manager->options(
		-usecache=true,
		-minify=true,
		-compress=false,
		-refresh=true,
		-paths=map(
			'yui'=server_webroot + '/assetmgr/yuicompressor.jar',
			'scriptcache'='/assetmgr/scripts/cache/',
			'stylecache'='/assetmgr/styles/cache/',
			'scriptbase'='/assetmgr/scripts/base/',
			'stylebase'='/assetmgr/styles/base/'
		),
		-subdomains=(: '/[^"]+?' = 'http://local.dev\\1')	
	);
	
	// add some assets
	asset_manager->add('scripts/jquery.corner.js');
	asset_manager->add('scripts/global.js');
	asset_manager->add('styles/global.css');
]
<html>
	<head>
		<title>Asset Manager Example</title>
	</head>
	<body>
		<h1>Hello, world.</h1>
	</body>
</html>
[asset_manager->cache]
						

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
107
108
109
110
111
112
113
114
115
116
define_type(
	'asset_manager',
	-prototype,
	-description='Automatically includes optimized scripts and styles in pages.'
);
	define_tag('oncreate');
		!var_defined('__amScripts') ? var('__amScripts') = array_unique;
		!var_defined('__amStyles') ? var('__amStyles') = array_unique;
	/define_tag;
	
	define_tag(
		'options',
		-opt='usecache', -type='boolean',
		-opt='minify', -type='boolean',
		-opt='compress', -type='boolean',
		-opt='refresh', -type='boolean',
		-opt='paths', -type='map',
		-opt='subdomains', -type='array'
	);
		!var_defined('__amOptions') ? var('__amOptions') = map;
		
		local('defaults') = map(
			'usecache' = true,
			'minify' = true,
			'compress' = false,
			'refresh' = false,
			'paths' = map(
				'yui'=server_webroot + '/lib/tools/yui_compressor.jar',
				'scriptcache'='/lib/scripts/cache/',
				'stylecache'='/lib/styles/cache/',
				'scriptbase'='/lib/scripts/base/',
				'stylebase'='/lib/styles/base/'
			),
			'subdomains' = array
		);
		
		iterate(#defaults->keys, local('i'));
			!local_defined(#i) ? local(#i) = #defaults->find(#i);
			$__amOptions->insert(#i = local(#i));
		/iterate;
					
		// load base assets
		self->loadbasejs;
		self->loadbasecss;
		
		// trigger automatic caching at end
		define_atend({ asset_manager->cache });
	/define_tag;
	
	define_tag('loadbasejs');
		local('base') = $__amOptions->find('paths')->find('scriptbase');
		local('files') = file_listdirectory(#base);
		
		iterate(#files, local('i'));
			!#i->beginswith('.') && #i->endswith('.js') ? $__amScripts->insert(#base + #i);
		/iterate;
	/define_tag;
	
	define_tag('loadbasecss');
		local('base') = @$__amOptions->find('paths')->find('stylebase');
		local('files') = file_listdirectory(#base);
		
		iterate(#files, local('i'));
			!#i->beginswith('.') && #i->endswith('.css') ? $__amStyles->insert(#base + #i);
		/iterate;
	/define_tag;
	
	define_tag('loadmodule', -req='path', -copy, -encodenone);
		local('stub') = string(#path)->removetrailing(#path->split('.')->last)&;
		file_exists(#stub + 'js') ? $__amScripts->insert(#stub + 'js');
		file_exists(#stub + 'css') ? $__amStyles->insert(#stub + 'css');
		return(include(#path));
	/define_tag;
	
	define_tag('add', -req='path');
		local('ext') = string(#path)->split('.')->last;
		#ext == 'js' && file_exists(#path) ? $__amScripts->insert(#path);
		#ext == 'css' && file_exists(#path) ? $__amStyles->insert(#path);
	/define_tag;
	
	define_tag('cache');
		var_defined('__amIsCached') && $__amIsCached ? return;
	
		local('scripts') = cache_assets(
			$__amScripts,
			-target=$__amOptions->find('paths')->find('scriptcache'),
			-usecache=$__amOptions->find('usecache'),
			-minify=$__amOptions->find('minify'),
			-compress=$__amOptions->find('compress'),
			-refresh=$__amOptions->find('refresh')
		);
		
		local('styles') = cache_assets(
			$__amStyles,
			-target=$__amOptions->find('paths')->find('stylecache'),
			-usecache=$__amOptions->find('usecache'),
			-minify=$__amOptions->find('minify'),
			-compress=$__amOptions->find('compress'),
			-refresh=$__amOptions->find('refresh')
		);
		
		content_body = string(content_body)->replace('</head>', #styles + '</head>')&replace('</body>', #scripts + '</body>')&;
		
		iterate($__amOptions->find('subdomains'), local('i'));
			#i->isa('pair') ? content_body = string_replaceregexp(
				content_body,
				-find='="(' + #i->first + ')"',
				-replace='="' + #i->second + '"',
				-ignorecase
			);
		/iterate;
		
		var('__amIsCached') = true;
	/define_tag;
/define_type;

 

Related Tags



Comments

none

Email:


Password:



Newest

Most Popular