Created
June 23, 2011 10:00
-
-
Save danhigham/1042286 to your computer and use it in GitHub Desktop.
CoffeeScript and Handlebars
This file contains 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
window.template_manager = new TemplateManager() | |
configuration = {} | |
template_manager.load_template "tabs", "/templates/_tabitems.handlebars", -> | |
markup = template_manager.transform "tabs", configuration | |
This file contains 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
class window.TemplateManager | |
constructor: () -> | |
@templates = new Array | |
@contexts = new Array | |
add_template: (name, template) -> | |
@templates[name] = template | |
load_template: (name, path, callback) -> | |
template = @templates[name] | |
console.log("Template key #{name} already exists") if template? | |
if !template? | |
$.ajax | |
url: path | |
success: (template_source) => | |
@add_template name, Handlebars.compile(template_source) | |
callback() if callback? | |
else | |
callback() if callback? | |
transform: (name, context) -> | |
# load the last context if the passed one is blank | |
context = @contexts[name] if !context? | |
template = @templates[name] | |
@contexts[name] = context | |
template context | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment