Is it possible to pass variables to a mustache partial

Morgan O'Neal picture Morgan O'Neal · Mar 27, 2013 · Viewed 10.3k times · Source

Lets say I have a mustache button partial like this

<button name="{{name}}" class="btn">{{title}}</button>

Is it possible somehow to set the title when calling the partial directly like this

<form>
....
{{> button|name=foo,title=Cancel}} {{> button|name=bar,title=Submit}}
</form>

This would make it much easier to create views instead of something like this and creating a hash for each button.

<form>
....
{{#buttons}}
    {{> button}}
{{/buttons}}
</form>

Answer

Ohad Sadan picture Ohad Sadan · Jun 27, 2013

I am not sure you can do that but I worked around that using mustache functions

javascript

var partial = "<div>{{value}}</div>";
var data = {some_data:[1,2,3]};
var items = {func:function (){
    return function (index){
        return mustache.render(partial,{value : data.some_data[index]});
    }
});
mustache.render(main,items);

mustache

{{#func}}1{{/func}}
{{#func}}2{{/func}}