简体   繁体   English

类似胡子的模板语言有扩展吗?

[英]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 . 我喜欢胡子式模板语言的最小化 - 我现在正在使用胡子和icanhasmustache,但我也检查了车把hogan

However I have a need to for an 'extends' type functionality, to allow a child to reference a particular parent template. 但是,我需要一个'extends'类型的功能,以允许子进程引用特定的父模板。 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. 我找不到关于如何在上面的任何一个中实现扩展的文档,但我已经看到(来自随机githib gists)其他人似乎正在这样做。

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 : 2016回答

If you're using express, the layout middleware takes a layout option which you may find useful. 如果您使用快速,则布局中间件采用您可能觉得有用的layout选项。

res.render('page', { layout: 'mylayout.jade' })

original answer : Very few JS libraries implement 'extends'-type functionality. 原始答案 :很少有JS库实现'扩展'类型的功能。

  • Nun is very mustache like, but server only (and no longer maintained) Nun非常像小胡子,但仅限服务器(并且不再维护)
  • Swig has extends, but isn't very mustache like. Swig已经延伸,但不是很像胡子。
  • Jade has extends and works in the browser, but isn't mustache like Jade已经扩展并在浏览器中工作,但不像胡子那样

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. 我决定使用Dust.JS ,因为它使用类似胡子的部分,可以在客户端和服务器上运行,并支持从子进程中覆盖父进程上的块,从而提供有效的扩展支持。

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. 我正在研究Nunjucks,它承诺解决一些问题以及支持继承。

EDIT: 编辑:

I have indeed adopted Nunjucks, it's pretty solid so far. 我确实收养了Nunjucks,到目前为止它非常稳固。 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. Twitter的Mustache,Hogan的实现似乎现在支持继承。

See this recent commit: Hogan 3. Add template inheritance... 看到最近的提交: Hogan 3.添加模板继承...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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