Skip to content

Instantly share code, notes, and snippets.

@simonoff
Forked from danhigham/application.coffee
Created March 5, 2012 18:10
Show Gist options
  • Save simonoff/1980012 to your computer and use it in GitHub Desktop.
Save simonoff/1980012 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