Skip to content

Instantly share code, notes, and snippets.

@jshirley
Created March 7, 2012 16:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jshirley/1994238 to your computer and use it in GitHub Desktop.
Save jshirley/1994238 to your computer and use it in GitHub Desktop.
How I manage my Handlebars templates in my YUI application.
#!/bin/bash
# This script should be switched up to be a buildy task, see
# https://github.com/mosen/buildy
#
# Each root/templates/(*)/(*).html should be baked to:
# -> template-$1-$2/template-$1-$2-min.js
#
# Then a template rollup file
ROLLUP="root/static/scripts/templates/template-rollup/template-rollup-min.js"
echo "YUI.add('template-rollup', function(Y) { var Handlebars = Y.namespace('Handlebars'), template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};" > $ROLLUP
for i in root/templates/*/*html
do
FILE=`basename $i | sed 's/\(.*\)\..*/\1/'`
DIR=`dirname root/templates/product/index.html`
TYPE=`basename $DIR`
mkdir -p root/static/scripts/templates/template-$TYPE-$FILE
script/handlebars $i -p --name template-$TYPE-$FILE > root/static/scripts/templates/template-$TYPE-$FILE/template-$TYPE-$FILE-min.js
script/handlebars $i --simple --name template-$TYPE-$FILE >> $ROLLUP
done
echo "}, '0.1.0', { requires: [ 'handlebars' ] });" >> $ROLLUP
#!/usr/bin/env node
var optimist = require('optimist')
.usage('Precompile handlebar templates.\nUsage: $0 template...', {
'f': {
'type': 'string',
'description': 'Output File',
'alias': 'output'
},
'k': {
'type': 'string',
'description': 'Known helpers',
'alias': 'known'
},
'o': {
'type': 'boolean',
'description': 'Known helpers only',
'alias': 'knownOnly'
},
'n': {
'type': 'string',
'description': 'Namespace for template',
'alias': 'name'
},
'm': {
'type': 'boolean',
'description': 'Minimize output',
'alias': 'min'
},
'p': {
'type': 'boolean',
'description': 'Register as a partial',
'alias': 'partial'
},
's': {
'type': 'boolean',
'description': 'Output template function only.',
'alias': 'simple'
},
'r': {
'type': 'string',
'description': 'Template root. Base value that will be stripped from template names.',
'alias': 'root'
}
})
.check(function(argv) {
var template = [0];
if (!argv._.length) {
throw 'Must define at least one template or directory.';
}
argv._.forEach(function(template) {
try {
fs.statSync(template);
} catch (err) {
throw 'Unable to open template file "' + template + '"';
}
});
})
.check(function(argv) {
if (argv.simple && argv.min) {
throw 'Unable to minimze simple output';
}
if (argv.simple && (argv._.length !== 1 || fs.statSync(argv._[0]).isDirectory())) {
throw 'Unable to output multiple templates in simple mode';
}
});
var fs = require('fs'),
handlebars = require('handlebars'),
basename = require('path').basename,
uglify = require('uglify-js');
var argv = optimist.argv,
template = argv._[0],
name = argv.name || template;
// Convert the known list into a hash
var known = {};
if (argv.known && !Array.isArray(argv.known)) {
argv.known = [argv.known];
}
if (argv.known) {
for (var i = 0, len = argv.known.length; i < len; i++) {
known[argv.known[i]] = true;
}
}
var output = [];
if (!argv.simple) {
output.push('YUI.add(\'' + name + '\', function(Y) {\n var Handlebars = Y.namespace("Handlebars"), template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};\n');
}
function processTemplate(template, root) {
var path = template,
stat = fs.statSync(path);
if (stat.isDirectory()) {
fs.readdirSync(template).map(function(file) {
var path = template + '/' + file;
if (/\.handlebars$/.test(path) || fs.statSync(path).isDirectory()) {
processTemplate(path, root || template);
}
});
} else {
var data = fs.readFileSync(path, 'utf8');
var options = {
knownHelpers: known,
knownHelpersOnly: argv.o
};
// Clean the template name
if (!root) {
template = basename(template);
} else if (template.indexOf(root) === 0) {
template = template.substring(root.length+1);
}
template = template.replace(/\.handlebars$/, '');
//if (argv.simple) {
// output.push(handlebars.precompile(data, options) + '\n');
//} else {
output.push('templates[\'' + name + '\'] = template(' + handlebars.precompile(data, options) + ');\n');
if ( argv.partial ) {
output.push('Handlebars.registerPartial(\'' + name + '\', templates[\'' + name + '\']);');
}
//}
}
}
argv._.forEach(function(template) {
processTemplate(template, argv.root);
});
// Output the content
if (!argv.simple) {
output.push("}, '0.1.0', { requires: [ 'handlebars' ] });");
}
output = output.join('');
if (argv.min) {
var ast = uglify.parser.parse(output);
ast = uglify.uglify.ast_mangle(ast);
ast = uglify.uglify.ast_squeeze(ast);
output = uglify.uglify.gen_code(ast);
}
if (argv.output) {
fs.writeFileSync(argv.output, output, 'utf8');
} else {
console.log(output);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment