Skip to content

Instantly share code, notes, and snippets.

@SiTaggart
Created May 5, 2012 08:29
Show Gist options
  • Save SiTaggart/2600935 to your computer and use it in GitHub Desktop.
Save SiTaggart/2600935 to your computer and use it in GitHub Desktop.
Underscore to moustache syntax and useage
<script>
//set underscore to have {{ }} terms
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g,
evaluate: /\{%([\s\S]+?)%\}/g
};
//set variables
var _subscriptionChannels = _.template($('#template').html()),
templateData = {
Owner: 'Channel 10',
channels: [
{
Name: 'test channel',
ImageUrl: '/path/to/image.jpg'
},
{
Name: 'test channel 2',
ImageUrl: '/path/to/image.jpg'
}
]
},
content = _subscriptionChannels(templateData);
//append compiled content
$('body').append(content);
</script>
<script type="text/template" id="template">
<ul class="om-available-channels-list om-available-channels-list-short">
<li class="om-available-channels-heading">
<h5>{{ Owner }}</h5>
</li>
{% _.each(channels, function(channel){ %}
<li>
<img height="34" alt="{{ channel.Name }}" src="{{ channel.ImageUrl }}">
</li>
{% }); %}
</ul>
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment