Created
October 21, 2011 15:40
-
-
Save alecperkins/1304147 to your computer and use it in GitHub Desktop.
Backbone template…template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
## 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