Reputation: 751
I'm using express + consolidate + mustache as template engine and I was wondering if there's a way to teach express to automatically load partials with matching filenames which I have not explicitly defined.
Example:
head.html:
---
<!DOCTYPE html>
<html><head><title></title></head><body>
index.html:
---
{{> head}}
{{content}}
{{> foot}}
foot.html:
---
</body></html>
app.js:
---
app.get('/', function(req, res) {
res.render('index.html', {
partials: {
head: 'head',
foot: 'foot'
}
});
});
As you can see, the head
and foot
partials are named exactly as their according filenames (minus .html
). Doing it that way works fine but I'm looking for an easy way to let express/mustache load partials automatically if there's a matching filename; without the need to specify my default partials in each and every function again. {{> example}}
-> looks if views/example.html
exists and loads it if example
is not defined in the partials object).
Is that possible?
Or is there at least an easy way to define default partials globally?
Upvotes: 1
Views: 422
Reputation: 2051
This is not possible with consolidate yet. We had the exact same problem (be able to call partials by their names without having to tell hogan/consolidate to load them everytime) and couldn't find a good module so we made our own, which we have been using in production for a few weeks: h4e ("Hogan for Express"). With it all partials are loaded automatically and you can also use layouts if you want to.
I think it should answer your need. Louis
Upvotes: 1