Closed enki closed 11 years ago
This is how I compile Hogan templates.
path = require('path')
assets = require('connect-assets')
hogan = require('hogan.js')
assets.jsCompilers.mustache =
namespace: "TEMPLATES"
match: /\.js$/
compileSync: (sourcePath, source) ->
assetName = path.basename(sourcePath, '.mustache')
compiled = hogan.compile(source, asString: true)
"(function() { window.#{@namespace} = window.#{@namespace} || {}; window.#{@namespace}['#{assetName}'] = #{compiled}; })();"
Thanks for that snippet @umairsiddique - great workaround.
@umairsiddique thank you for the snippet. It was very helpful in figuring out how to add a custom compiler.
Here is a snippet for adding a compiler for underscore templates.
assets.jsCompilers.jst = {
match: /\.js$/,
compileSync: function(sourcePath, source) {
var compiled = _.template(source).source,
assetName = path.basename(sourcePath, '.jst');
return "(function() { window.TEMPLATES = window.TEMPLATES || {}; window.TEMPLATES['" + assetName + "'] = " + compiled + "; })();";
}
};
You can then just name your templates with extension .jst and they will work.
i'm monkey patching snockets to provide support for requiring client-side jade templates (to be used with jade.js):
snockets = require('connect-assets/nodemodules/snockets/lib/snockets') snockets.compilers.jade = match: /.js$/
compileSync: (sourcePath, source) -> "jade" + path.basename(sourcePath, '.jade') + " = jade.compile(" + JSON.stringify(source) + ", {filename: " + JSON.stringify(sourcePath) + "});"
maybe you just want to support a few template engines like that out of the box, or at the very least support for .txt, so that you can just compile them yourself without having to write inline template code
non-sequitur: also i found it annoying that package.json refers to main as "main": "lib/snockets.js" - that meant i had to cake after every modification. not much point to it when depending on coffee-script anyway