Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mustache-like templating language with extends?

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.

like image 771
mikemaccana Avatar asked Dec 06 '22 15:12

mikemaccana


2 Answers

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.

  • Nun is very mustache like, but server only (and no longer maintained)
  • Swig has extends, but isn't very mustache like.
  • Jade has extends and works in the browser, but isn't mustache like

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.

like image 103
mikemaccana Avatar answered Dec 17 '22 05:12

mikemaccana


I'm looking into Nunjucks, which promises to fix some issues as well as support inheritance.

  • http://jlongster.com/2012/09/20/nunjucks.html
  • https://github.com/jlongster/nunjucks

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.

like image 41
Aram Kocharyan Avatar answered Dec 17 '22 04:12

Aram Kocharyan