Skip to content

Commit

Permalink
Merge pull request #3 from gbouthenot/precompiler_support
Browse files Browse the repository at this point in the history
Add precompiler output support
  • Loading branch information
balupton committed Jan 19, 2013
2 parents 88e1eee + 4964a46 commit e9b751d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
32 changes: 31 additions & 1 deletion 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
Expand Down Expand Up @@ -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
Expand Down
33 changes: 31 additions & 2 deletions src/handlebars.plugin.coffee
Expand Up @@ -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
Expand All @@ -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 e9b751d

Please sign in to comment.