From 0b996eb94c2a80f05a90a362df81e678e736b8c3 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Thu, 13 Feb 2014 00:15:58 -0500 Subject: [PATCH 1/2] Add the standard template helpers --- src/handlebars.plugin.coffee | 15 +++++++--- .../standard-template-helpers.html.hbs | 29 +++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 test/src/documents/standard-template-helpers.html.hbs diff --git a/src/handlebars.plugin.coffee b/src/handlebars.plugin.coffee index 85a8026..2841eaf 100644 --- a/src/handlebars.plugin.coffee +++ b/src/handlebars.plugin.coffee @@ -15,19 +15,26 @@ module.exports = (BasePlugin) -> super docpad = @docpad config = @config - handlebars = @handlebars = require('handlebars') + @handlebars = require('handlebars') @precompileOpts = @config.precompileOpts || {} + # Generate Before + generateBefore: -> + # Add the template helper as a Handlebars helper + for own key,value of @docpad.getTemplateData() + if Object::toString.call(value) is '[object Function]' + @handlebars.registerHelper(key, value) + # Add helpers, if defined in docpad.cson if @config.helpers for own name,helper of @config.helpers - handlebars.registerHelper(name, helper) + @handlebars.registerHelper(name, helper) # Add partials, if defined in docpad.cson if @config.partials for own name,partial of @config.partials - handlebars.registerPartial(name, partial) + @handlebars.registerPartial(name, partial) # Render some content render: (opts) -> @@ -48,7 +55,7 @@ module.exports = (BasePlugin) -> 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") diff --git a/test/src/documents/standard-template-helpers.html.hbs b/test/src/documents/standard-template-helpers.html.hbs new file mode 100644 index 0000000..6b9e3a1 --- /dev/null +++ b/test/src/documents/standard-template-helpers.html.hbs @@ -0,0 +1,29 @@ +

Standard Template Helpers

+ +

include(relativePath)

+ +{{{include handlebars.html.hbs}}} + + +

getEnvironment()

+ +{{getEnvironment}} + + +

getEnvironments()

+ +{{#each getEnvironments}} + {{this}} +{{/each}} + + +

getBlock(blockname)s

+ +{{{getBlock "scripts"}}} +{{{getBlock "styles"}}} +{{{getBlock "meta"}}} + + +

getPath(path,parentPath)

+ +{{getPath "handlebars-helper.html" "test"}} From be89053ff943cf0c491fae7c2ef88b5110266d1e Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Thu, 13 Feb 2014 00:24:34 -0500 Subject: [PATCH 2/2] Fix the tests - fixes #2 --- test/docpad.cson | 5 +++- .../standard-template-helpers.html | 29 +++++++++++++++++++ .../standard-template-helpers.html.hbs | 12 ++++---- 3 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 test/out-expected/standard-template-helpers.html diff --git a/test/docpad.cson b/test/docpad.cson index 75c00e7..579ccc3 100644 --- a/test/docpad.cson +++ b/test/docpad.cson @@ -7,4 +7,7 @@ partials: partial1: '

{{document.example}}

' partial2: 'Scroll up' -} \ No newline at end of file + + # Increase the render passes so that the include helper works. + renderPasses: 2 +} diff --git a/test/out-expected/standard-template-helpers.html b/test/out-expected/standard-template-helpers.html new file mode 100644 index 0000000..7b73ba7 --- /dev/null +++ b/test/out-expected/standard-template-helpers.html @@ -0,0 +1,29 @@ +

Standard Template Helpers

+ +

include "handlebars-helper.html.hbs"

+ +

Testing Helpers

+ + +

getEnvironment

+ +development + + +

getEnvironments

+ + + development + + + +

getBlock blockname

+ +[object Object] +[object Object] +[object Object] + + +

getPath "handlebars-helper.html" "test"

+ +test/handlebars-helper.html diff --git a/test/src/documents/standard-template-helpers.html.hbs b/test/src/documents/standard-template-helpers.html.hbs index 6b9e3a1..af29eac 100644 --- a/test/src/documents/standard-template-helpers.html.hbs +++ b/test/src/documents/standard-template-helpers.html.hbs @@ -1,29 +1,29 @@

Standard Template Helpers

-

include(relativePath)

+

include "handlebars-helper.html.hbs"

-{{{include handlebars.html.hbs}}} +{{{include "handlebars-helper.html.hbs"}}} -

getEnvironment()

+

getEnvironment

{{getEnvironment}} -

getEnvironments()

+

getEnvironments

{{#each getEnvironments}} {{this}} {{/each}} -

getBlock(blockname)s

+

getBlock blockname

{{{getBlock "scripts"}}} {{{getBlock "styles"}}} {{{getBlock "meta"}}} -

getPath(path,parentPath)

+

getPath "handlebars-helper.html" "test"

{{getPath "handlebars-helper.html" "test"}}