Skip to content

Instantly share code, notes, and snippets.

@davidgtonge
Created September 20, 2011 06:29
Show Gist options
  • Save davidgtonge/1228494 to your computer and use it in GitHub Desktop.
Save davidgtonge/1228494 to your computer and use it in GitHub Desktop.
Simple CoffeeScript Templating
# Simple Coffeescript Templating
# Global Object to hold the templates
window.simpleCoffeeTemplates =
# An example template
example: (a) ->
"""
<div>
<h1 class='header'>#{a.header}</h1>
<h2 class='header2'>#{a.header2}</h2>
<h3 class='header3'>#{a.header3}</h3>
<h4 class='header4'>#{a.header4}</h4>
<h5 class='header5'>#{a.header5}</h5>
<h6 class='header6'>#{a.header6}</h6>
<ul>
#{simpleCoffeeTemplates.loop(a.list, 'li')}
</ul>
</div>
"""
li: (a) ->
"<li class='item'>#{a}</li>"
# Example loop method that will loop through an array
loop: (data, template) ->
out = ""
out += simpleCoffeeTemplates[template] a for a in data
out
# Example loop method to loop through an object
loopObj: (data, template) ->
out = ""
out += simpleCoffeeTemplates[template] a for b, a of data
out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment