Permalink
Browse files

Add precompiler output support

  • Loading branch information...
1 parent 88e1eee commit 4964a464f5006fa056e988ebdf911ce5a9679480 @gbouthenot gbouthenot committed Jan 4, 2013
Showing with 62 additions and 3 deletions.
  1. +31 −1 README.md
  2. +31 −2 src/handlebars.plugin.coffee
View
32 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
View
33 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

0 comments on commit 4964a46

Please sign in to comment.