Skip to content

Instantly share code, notes, and snippets.

@rx
Forked from cmorss/gist:1076315
Created August 1, 2011 14:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rx/1118201 to your computer and use it in GitHub Desktop.
Save rx/1118201 to your computer and use it in GitHub Desktop.
Jammit Handlebar Template funciton
Handlebars.template = function(templateString) {
return function () {
if (arguments.length < 1) {
// With no arguments, return the raw template -- useful for rendering
// partials.
return templateString;
} else {
Handlebars.templates = Handlebars.templates || {}
Handlebars.templates[templateString] = Handlebars.templates[templateString] || Handlebars.compile(templateString);
return Handlebars.templates[templateString](arguments[0], arguments[1]);
}
};
};
@rx
Copy link
Author

rx commented Aug 1, 2011

I forked this because it did not work. I changed the Handlebar references to Handlebars and presto. I hope someone else can use this info.

@arikfr
Copy link

arikfr commented Aug 22, 2011

I wish I noticed your fork earlier... could've save me some time getting to the same solutions... :-)
Thanks!

@rx
Copy link
Author

rx commented Aug 22, 2011 via email

@cmorss
Copy link

cmorss commented Aug 22, 2011

Thanks for pointing out my syntax error - I fixed the original just in case anyone else runs across it.

@danielgrippi
Copy link

hmm, where does one put this?

@cmorss
Copy link

cmorss commented Feb 5, 2012

You can put it anyplace after the handlebars script tags and before you actually need a template.

@danielgrippi
Copy link

what version of handlebars are you running this against?

@danielgrippi
Copy link

this actually seems to do the trick with the latest version of handlebars: template_function: Handlebars.compile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment