Jade包含条件来检查实际页面

有没有办法检查你在Jade中的哪个页面,并在该页面中包含一些内容(部分页面或特定的CSS样式)? 例如:

如果我在homepage ,那么只包括HOMEPAGE-head.jade

否则 – 包括NORMAL-head.jade

这里是一个上下文的例子:

 doctype html html body if HOMEPAGE include ./includes/HOMEPAGE-head.jade else include ./includes/NORMAL-head.jade h1 My Site p Welcome to my super lame site. include ./includes/foot.jade 

谢谢!

我知道有两种方法。

选项A:2个布局和扩展

制作两个布局: layout.jadelayout-homepage.jade ,相应地更改include行。 大多数页面将extends layout ,但是index.jadeextends layout-homepage

选项B:variables块

layout.jade

 - var HOMEPAGE = false; block variables doctype html html body if HOMEPAGE include ./includes/HOMEPAGE-head.jade else include ./includes/NORMAL-head.jade h1 My Site p Welcome to my super lame site. include ./includes/foot.jade 

然后在index.jade

 block variables - HOMEPAGE = true; h1 This is your home page template... 

所有其余的页面将默认为HOMEPAGE = false因此它们不需要任何更改即可使此方法有效。

或者,你可以构build你的翡翠使用inheritance来实现你想要的。

例如,

layout.jade:

 doctype html html body block header block content h1 My Site p Welcome to my super lame site. block footer include ./includes/foot.jade 

homepage.jade:

 extends ./layout.jade block header include ./includes/HOMEPAGE-head.jade 

normal.jade:

 extends ./layout.jade block header include ./includes/NORMAL-head.jade 

然后让所有正常的页面使用normal.jade和你的主页来使用homepage.jade