Skip to content

Instantly share code, notes, and snippets.

@anttih
Created August 28, 2012 11:52
Show Gist options
  • Save anttih/3497493 to your computer and use it in GitHub Desktop.
Save anttih/3497493 to your computer and use it in GitHub Desktop.
Simple templating in LiveScript
toHtml = (x) ->
if $.isArray x
if typeof x[0] is not \string then return map(toHtml, x).join('')
switch x.length
case 3
"<#{x[0]} #{objToAttrs(x[1])}>#{toHtml(x[2])}</#{x[0]}>"
case 2
"<#{x[0]}>#{toHtml(x[1])}</#{x[0]}>"
case 1
"<#{x[0]}/>"
else
x
where
objToAttrs = (o) -> map(([key, value]) -> "#{key}=\"#{value}\"", zip(keys(o), values(o))).join(' ')
# usage:
#
# toHtml([\li {class: \target} [\a {href: "http://asdf.com" "data-target-id": 12} \string]])
# -> <li class="target"><a href="http://asdf.com" data-target-id="12">string</a></li>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment