Reputation: 123710
I like the minimal-ness of mustache-style templating languages - I'm currently using mustache and icanhasmustache, but I've also checked out handlebars and hogan.
However I have a need to for an 'extends' type functionality, to allow a child to reference a particular parent template. I can't find documentation on how extends are implemented in any of the above, but I've seen (from random githib gists) that other people seem to be doing it.
Note: I'm aware of the existence of includes (sometimes called partials), however these seems to be for a parent to reference a particular child. That's the opposite of what I'm looking for - the child template in this case is the real 'base' document, the parent merely incidental, so I want the child to control the relationship.
Upvotes: 4
Views: 2961
Reputation: 123710
2016 answer:
If you're using express, the layout middleware takes a layout
option which you may find useful.
res.render('page', { layout: 'mylayout.jade' })
original answer: Very few JS libraries implement 'extends'-type functionality.
I settled on Dust.JS, as it uses mustache-like sections, works on client and server, and supports overriding blocks on the parent from the child, giving effective extends support.
See the dust documentation, 'Blocks and Inline Partials' section:
{>base_template/}
{<title}
Child Title
{/title}
{<main}
Child Content
{/main}
Overriding the 'title' and 'main' sections from the parent template, keeping the surrounding contents.
Upvotes: 8
Reputation: 20431
I'm looking into Nunjucks, which promises to fix some issues as well as support inheritance.
EDIT:
I have indeed adopted Nunjucks, it's pretty solid so far. One limitation I've encountered is that you can't specify multiple folders for precompiling, but I wrote a script to allow it.
Upvotes: 5
Reputation: 11706
Twitter's implementation of Mustache, Hogan, seems to support inheritance now.
See this recent commit: Hogan 3. Add template inheritance...
Upvotes: 2