Last Updated: February 25, 2016
·
15.96K
· fragphace

Partial variables in Mustache?

No. You can't render partial with predefined variables. But you can do something even better! Lets assume you have following code:

page.html

{{> partial}}
{{> partial}}

partial.html

{{#list}}
    {{item}}
{{/list}}

index.js

Mustache.render(page, {
    list: [{ item: 'foo' }]
}, { 
    partial: partial 
}); 

This will generate: foo foo

Here's the question: How to render partials with different lists?. Fortnately, there's a clean solution:

page.html

{{#firstList}}
    {{> partial}}
{{/firstList}}

{{#secondList}}
    {{> partial}}
{{/secondList}}

partial.html

{{#list}}
    {{item}}
{{/list}}

index.js

Mustache.render(page, {
    firstList: {
        list: [{ item: 'foo' }]
    }, 
    secondList: {
        list: [{ item: 'bar' }]
    }
}, {
    partial: partial 
}); 

This will produce: foo bar. Done :)