niectg
Last Updated: February 25, 2016
·
7.14K
· fragphace
20842946074d9b78d6004bb7cb18c603

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 :)

Say Thanks
Respond