Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the standard template helpers #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/handlebars.plugin.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


# 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) ->
Expand All @@ -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")
Expand Down
5 changes: 4 additions & 1 deletion test/docpad.cson
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
partials:
partial1: '<h1>{{document.example}}</h1>'
partial2: '<a href="#">Scroll up</a>'
}

# Increase the render passes so that the include helper works.
renderPasses: 2
}
29 changes: 29 additions & 0 deletions test/out-expected/standard-template-helpers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<h1>Standard Template Helpers</h1>

<h2>include "handlebars-helper.html.hbs"</h2>

<h1>Testing Helpers</h1>


<h2>getEnvironment</h2>

development


<h2>getEnvironments</h2>


development



<h2>getBlock blockname</h2>

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


<h2>getPath "handlebars-helper.html" "test"</h2>

test/handlebars-helper.html
29 changes: 29 additions & 0 deletions test/src/documents/standard-template-helpers.html.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<h1>Standard Template Helpers</h1>

<h2>include "handlebars-helper.html.hbs"</h2>

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


<h2>getEnvironment</h2>

{{getEnvironment}}


<h2>getEnvironments</h2>

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


<h2>getBlock blockname</h2>

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


<h2>getPath "handlebars-helper.html" "test"</h2>

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