简体   繁体   中英

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.

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.

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.

Twitter's implementation of Mustache, Hogan, seems to support inheritance now.

See this recent commit: Hogan 3. Add template inheritance...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM