Skip to content

Instantly share code, notes, and snippets.

@doowb
Forked from jonschlinkert/helpers.coffee
Created July 4, 2013 20:01
Show Gist options
  • Save doowb/5929936 to your computer and use it in GitHub Desktop.
Save doowb/5929936 to your computer and use it in GitHub Desktop.
# Usage: {{ include [partial] }}
module.exports.include = include = (template, options) ->
partial = Handlebars.partials[template]
if (typeof partial is "string")
partial = Handlebars.compile(partial)
Handlebars.partials[template] = partial
return Utils.safeString('Partial **' + template + '** not found.') unless partial
context = _.extend({}, this, options.hash)
Utils.safeString partial(context)
# 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 = (template) ->
values = Array::slice.call(arguments, 1)
opts = values.pop()
until done
value = values.pop()
if value
template = template.replace(/:[^\.]+/, value)
else
done = true
partial = Handlebars.partials[template]
if (typeof partial is "string")
partial = Handlebars.compile(partial)
Handlebars.partials[template] = partial
return Utils.safeString('Partial **' + template + '** not found.') unless partial
context = _.extend({}, opts.context or this, _.omit(opts, "context", "fn", "inverse"))
Utils.safeString partial(context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment