Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
165
rated 0 times [  167] [ 2]  / answers: 1 / hits: 8725  / 11 Years ago, fri, december 6, 2013, 12:00:00

I have a mustache template and I would like to call some function on the mustache variables ({{name}} in this case). Specifically, I want to call toLowerCase() method on the name variable.



<tbody>
<script id=mytemplate type=text/template>
{{#cat}}
<tr data-index={{age}}-{{name}}></tr>
{{/cat}}
</script>
</tbody>


I tried looking in the mustache docs but I couldn't find out how to do this. I tried doing




  1. <tr data-index={{age}}-{{name.toLowerCase()}}></tr>

  2. <tr data-index={{age}}-{{name}}.toLowerCase()></tr>



But I'm not getting what I expect. I render the template with this code which gets triggered on document ready.



$(function() {
$.getJSON('/cats.json', function(data){
var template = $(#mytemplate).html();
var view = Mustache.to_html(template, data);
$(tbody).html(view);
});
})

More From » jquery

 Answers
12

you need to pass the function as part of the data, like so:



$(function() {
$.getJSON('/cats.json', function(data){
data.lower = function () {
return function (text, render) {
//wrong line return render(text.toLowerCase());
return render(text).toLowerCase();
}
};
var template = $(#mytemplate).html();
var view = Mustache.to_html(template, data);
$(tbody).html(view);
});
})


and the template:



<tr data-index={{age}}-{{#lower}}{{name}}{{/lower}}></tr>

[#49780] Thursday, December 5, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
joseluispauld

Total Points: 13
Total Questions: 132
Total Answers: 98

Location: Venezuela
Member since Sat, Apr 24, 2021
3 Years ago
;