Skip to content

Instantly share code, notes, and snippets.

@zhannes
Created January 7, 2013 16:12
Show Gist options
  • Save zhannes/4476148 to your computer and use it in GitHub Desktop.
Save zhannes/4476148 to your computer and use it in GitHub Desktop.
mustache.js helper functions - Add a property on your data object. Use the property name to reference the helper within your template. Inside the helper, you can reference the current object as `this`.
<script>
var people = getData(); // however you get your data ...
/* structure
{
people : [
{ name : "Shiva Khomini Somar Kondar Krohm" },
{ name : ... }
]
}
*/
// store a function in a property, 'shortName'
people.shortName = function(){
return this.name.substring(0,19) + '...';
};
/* `this` value depends on what your template is doing when you call the helper.
We iterate over people, So we'll call it inside that loop
*/
</script>
// your awesome stache template
<script type="text/html" id="my-template">
{{#people}}
<h3>{{name}}</h3>
<span>{{people.shortName}}</span>
{{/people}}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment