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 :)
Written by Pawel Maciejewski
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Js
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#