Skip to content

Instantly share code, notes, and snippets.

@danhigham
Created June 23, 2011 10:00
Show Gist options
  • Save danhigham/1042286 to your computer and use it in GitHub Desktop.
Save danhigham/1042286 to your computer and use it in GitHub Desktop.
CoffeeScript and Handlebars
window.template_manager = new TemplateManager()
configuration = {}
template_manager.load_template "tabs", "/templates/_tabitems.handlebars", ->
markup = template_manager.transform "tabs", configuration
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