Created
June 24, 2013 23:50
-
-
Save jonschlinkert/5854780 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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