diff --git a/README.md b/README.md index 6b74ecd..3c8d72a 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Handlebars Plugin for DocPad Adds support for the [Handlebars](http://handlebarsjs.com/) templating engine to [DocPad](https://docpad.org) -Convention: `.anything.handlebars|hbs|hb` +Convention: `.inlinejs|js|anything.handlebars|hbs|hb` ## Install @@ -31,6 +31,36 @@ Here is a small example: } +## Usage as precompiler + +If the document extension is `.inlinejs|js.handlebars|hbs|hb`, the plugin will produce a precompiled template. +In that, case, you can customise the ouput in the docpad settings: + + { + precompileOpts: + wrapper: "default" + # wrapper should be "default", "amd" or "none" (please note that "none" is probably not what you want + # "none": This option produces non-uglifable, so use "*.inlinejs.handlebars" extension + # function (Handlebars,depth0,helpers,partials,data) { + # ... + # } + # "default" : + # (function() { + # var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {}; + # templates['test'] = template(function (Handlebars,depth0,helpers,partials,data) { + # ... + # }) + # })(); + # "amd": + # define(['handlebars'], function(Handlebars) { + # var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {}; + # templates['test'] = template(function (Handlebars,depth0,helpers,partials,data) { + # ... + # }); + # }); + } + + ## History You can discover the history inside the `History.md` file diff --git a/src/handlebars.plugin.coffee b/src/handlebars.plugin.coffee index 3458882..d187170 100644 --- a/src/handlebars.plugin.coffee +++ b/src/handlebars.plugin.coffee @@ -17,6 +17,7 @@ module.exports = (BasePlugin) -> config = @config handlebars = @handlebars = require('handlebars') + @precompileOpts = @config.precompileOpts || {} # Add helpers, if defined in docpad.cson if @config.helpers for own name,helper of @config.helpers @@ -30,8 +31,36 @@ module.exports = (BasePlugin) -> # Render some content render: (opts) -> # Prepare - {inExtension,templateData,content} = opts + {inExtension,outExtension,templateData,content} = opts handlebars = @handlebars if inExtension in ['hb', 'hbs', 'handlebars'] - opts.content = handlebars.compile(opts.content)(templateData) + if outExtension in ['js', 'inlinejs'] + output = @handlePrecompileOpts(opts) + else + output = handlebars.compile(opts.content)(templateData) + opts.content = output + + handlePrecompileOpts: (opts) -> + argv = @precompileOpts + argv.wrapper ?= "default" + argv.amdPath ?= "" + + pre = post = ""; + # slug for {src}/tpl/a/abc/test.js.handlebars is "tpl-a-abc-test" + templateName = opts.file.attributes.slug; + if (argv.wrapper is "amd") + pre += 'define([\'' + argv.amdPath + 'handlebars\'], function(Handlebars) {\n' + if (argv.wrapper is "default") + pre += '(function() {\n' + if (argv.wrapper in [ "default", "amd" ]) + pre += ' var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};\n' + pre += 'templates[\'' + templateName + '\'] = template(' + post += ');\n' + + if (argv.wrapper is "amd") + post += '});' + if (argv.wrapper is "default") + post += '})();' + + return pre + @handlebars.precompile(opts.content) + post