Created
January 7, 2013 16:12
-
-
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`.
This file contains 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
<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