Skip to content

Instantly share code, notes, and snippets.

@alecperkins
Created October 21, 2011 15:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alecperkins/1304147 to your computer and use it in GitHub Desktop.
Save alecperkins/1304147 to your computer and use it in GitHub Desktop.
Backbone template…template
###
## Partials
Included in templates using CoffeeScript's string interpolation:
partial = "<partial>content {{ foo }}</partial>"
template = """
<template>
<stuff>#{ partial }</stuff>
<morestuff></morestuff>
</template>
"""
Partials can include Underscore templating in them, since they become part of
the template string before compiled by `_.template`.
###
partials =
some_partial: "<button class="foo" title="Foo this Bar">Foo</button>"
###
## Templates
The templates are defined as keys and strings in an object, which is then
iterated over and compiled through the _.template function, and exported to
the app module.
###
templates =
TemplateName: """
<div>{{ content }}</div>
#{ partials.some_partial }
<ul>
{% _(objects).each(function(obj){ %}
<li>{{ obj }}</li>
{% }); %}
</ul>
"""
###
## Settings
Use Django-style templating:
{{ interpolation }}
{% evaluation %}
###
_.templateSettings =
evaluate : /\{%(.+?)%\}/g
interpolate : /\{\{(.+?)\}\}/g
###
## Compile & Export
###
for name, template of templates
templates[name] = _.template(template)
window.Module.templates = templates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment