Smarty 3+ plugin to bundle and minify js and/or css files.
This Smarty plugin uses Matthias Mullie's Minify library to do it's minifications and bundling., (*1)
It automatically checks for (JS/CSS) file/code changes and then rebuild's the minified/bundled output file., (*2)
Table of Contents, (*3)
- Installation
- Parameters
- Usage/Examples
, (*4)
Installation
Use Composer to include the library into your project:, (*5)
$ composer.phar require jrohde/smarty-ass
Now add the plugin dir to your Smarty instance:, (*6)
$smarty = new \Smarty();
$smarty->addPluginsDir('./path/to/vendor/rohdej/smarty-ass/');
You can check if the correct path was added by using:, (*7)
var_dump($smarty->getPluginsDir());
, (*8)
Parameters
-
in (array) - absolute path('s) to local JS or CCS files, inline or style/ tags and/or external JS or css files.
-
out (string) - absolute path to the minified and bundled file output (must be writable by the webserver/php user!). This parameter is optional and defaults to '/assets/(js/css)/${smarty_template_name}_combined.(css/js)'.
-
ttl (integer) - time to live in of cached files in seconds. This is the maximum time a minified/combined file may exist before a complete rebuild. Note that when source files are modified the output file is regenerated, regardless of this value. This parameter is optional and defaults to '31536000' (a.k.a: 1 year).
-
gzip (boolean) - gzip's the output file. Make sure your PHP/Webserver supports this! This parameter is optional and defaults to 'false'
-
debug (boolean) - disables compilation and just output all as normal if set to: 'true'. This parameter is optional and defaults to 'false'
, (*9)
Usage (in Smarty 3+ templates)
{ass in=['file1.js','file2.js'] out='/assets/combined_and_minified.js' ttl='3600' gzip='false' debug=false}
You may also use external js/css/cdn's like:, (*10)
{ass in=[
'https://code.jquery.com/jquery-git.min.js',
'https://code.jquery.com/ui/1.12.1/jquery-ui.min.js'
] out='/assets/combined_and_minified.js' ttl='3600' gzip='false' debug=false}
Use/add inline css or js (but make sure to escape single/double quotes!):, (*11)
{ass in=["
<script>
var cool = 'awesome';
</script>
"] out='/assets/combined_and_minified.js' ttl='3600' gzip='false' debug=false}
Or even mix it all together:, (*12)
{ass in=[
'https://code.jquery.com/jquery-git.min.js',
'https://code.jquery.com/ui/1.12.1/jquery-ui.min.js',
'file1.js',
'file2.js',
'<script>
var cool = \'awesome\';
</script>'
] out='/assets/combined_and_minified.js' gzip='true'}