Skip to content

Instantly share code, notes, and snippets.

@doowb
Forked from jonschlinkert/helpers.coffee
Last active December 18, 2015 22:38
Show Gist options
  • Save doowb/5855504 to your computer and use it in GitHub Desktop.
Save doowb/5855504 to your computer and use it in GitHub Desktop.
assembleOptions = options #this is assuming these helpers are in the register function that takes in an options object from assemble
minimatch = require 'minimatch'
matchPartials = (src, search) ->
files = grunt.files.expand src
partials = files.filter(minimatch.filter(search))
partials
# Usage: {{ include [partial] }}
module.exports.include = include = (search, options) ->
rtn = ''
partials = matchPartials assembleOptions.partials, search
_.forEach partials, (name) ->
name = path.filename name
partial = Handlebars.partials[name]
if (typeof partial is "string")
partial = Handlebars.compile(partial)
Handlebars.partials[name] = partial
return Utils.safeString('Partial **' + name + '** not found.') unless partial
context = _.extend({}, this, options.hash)
rtn += partial(context)
Utils.safeString rtn
# Adds support for passing arguments to partials. Arguments are merged with
# the context for rendering only (non destructive).
# Use `:token` syntax to replace parts of the template path.
# Tokens are replaced in order.
# USAGE: {{partial 'path.to.partial' context=newContext foo='bar' }}
# USAGE: {{partial 'path.:1.:2' replaceOne replaceTwo foo='bar' }}
module.exports.partial = partial = (search) ->
values = Array::slice.call(arguments, 1)
opts = values.pop()
# until done
# value = values.pop()
# if value
# template = template.replace(/:[^\.]+/, value)
# else
# done = true
rtn = ''
partials = matchPartials assembleOptions.partials, search
_.forEach partails, (name) ->
name = path.filename(name)
partial = Handlebars.partials[name]
if (typeof partial is "string")
partial = Handlebars.compile(partial)
Handlebars.partials[name] = partial
return Utils.safeString('Partial **' + name + '** not found.') unless partial
context = _.extend({}, opts.context or this, _.omit(opts, "context", "fn", "inverse"))
rtn += partial(context)
Utils.safeString rtn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment