1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2025-07-12 13:11:08 +01:00

feat: improve template helper wiring

This commit is contained in:
hacksalot
2018-02-07 05:49:02 -05:00
parent 2346562428
commit 38a073b09a
4 changed files with 27 additions and 14 deletions

View File

@ -17,23 +17,28 @@ Register useful Handlebars helpers.
@method registerHelpers
###
module.exports = ( theme, opts ) ->
module.exports = ( theme, rez, opts ) ->
helpers.theme = theme
helpers.opts = opts
helpers.type = 'handlebars'
# Prepare generic helpers for use with Handlebars. We do this by wrapping them
# in a Handlebars-aware wrapper which calls the helper internally.
wrappedHelpers = _.mapObject helpers, ( hVal, hKey ) ->
if _.isFunction hVal
return _.wrap hVal, (func) ->
args = Array.prototype.slice.call arguments
args.shift() # lose the 1st element (func)
args.pop() # lose the last element (the Handlebars options hash)
func.apply @, args
args.shift() # lose the 1st element (func) [^1]
#args.pop() # lose the last element (HB options hash)
args[ args.length - 1 ] = rez # replace w/ resume object
func.apply @, args # call the generic helper
hVal
, @
HANDLEBARS.registerHelper wrappedHelpers
# Prepare Handlebars-specific helpers - "blockHelpers" is really a misnomer
# since any kind of Handlebars-specific helper can live here
HANDLEBARS.registerHelper blockHelpers
# Register any theme-provided custom helpers...
@ -64,3 +69,9 @@ module.exports = ( theme, opts ) ->
inner: ex
glob: curGlob, exit: true
return
# [^1]: This little bit of acrobatics ensures that our generic helpers are
# called as generic helpers, not as Handlebars-specific helpers. This allows
# them to be used in other templating engines, like Underscore. If you need a
# Handlebars-specific helper with normal Handlebars context and options, put it
# in block-helpers.coffee.