How can I use a nested template within mustache? Is there a way to do the same?
var tmpl="{{#data}}
{{values}}
Name: {{name}}
//{{another_templ({{name.value}})}}
{{/values}}
{{/data}}"
Hope you guys got the question. I have not added the escape character for js validity since code is split across different lines.
The Mustache templates consist of tag names surrounded by { { } } (which resemble mustaches – hence the name) and are backed by a model object containing the data for the template.
Mustache is a logic-less template syntax. It can be used for HTML, config files, source code - anything. It works by expanding tags in a template using values provided in a hash or object. We call it "logic-less" because there are no if statements, else clauses, or for loops.
Mustache is a logic-less templating system. It permits you to use pre-written text files with placeholders that will be replaced at run-time with values particular to a given request.
A logic-less template is a template that contains holes for you to fill, and not how you fill them. The logic is placed elsewhere and mapped directly to the template. This separation of concerns is ideal because then the template can easily be built with different logic, or even with a different programming language.
You could use a lambda to nest the template:
function nested_template(template_string, translate) {
return function() {
return function(text, render) {
return Mustache.to_html(template_string, translate(render(text)));
};
};
}
var template_string =
"{{#data}}"+
"{{values}}"+
"Name: {{name}}"+
"{{#another_templ}}{{name}}{{/another_templ}}"+
"{{/values}}"+
"{{/data}}";
var another_template_string = "<b>{{name}}</b>"; // for example
var view = {
data: {
values: {
name: "Test"
}
},
another_templ: nested_template(another_template_string, function(text) {
return {name: text};
});
};
var result = Mustache.to_html(template_string, view);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With