无法使用Jade模板包含相对path文件

当我试图将文件包含在同一个文件夹中时收到以下错误:

the "filename" option is required to use "include" with "relative" paths 

有两个文件:

index.jade

list_of_items.jade

 .content-container .row .col-lg-10.col-lg-offset-1.col-xs-12 .row .col-xs-3 include list_of_items .col-xs-9 include content 

我试图使用基path,但后来收到以下错误:

 the "basedir" option is required to use "include" with "absolute" paths 

基本path的代码如下所示:

 .content-container .row .col-lg-10.col-lg-offset-1.col-xs-12 .row .col-xs-3 include /User/project/list_of_items .col-xs-9 include content 

我完全不知所措 有什么地方有我失踪的另一个设置? 感觉这应该是一个超级简单的东西。 我错过了什么?

你正在使用jade.compile() ,对不对? 如错误消息所述,您没有传递filename选项。 既然你只是给一个string编译成Jade,它不知道在哪里寻找包含的文件。

脚本和玉文件在同一个文件夹中的例子。 index.jade文件包含使用include foo另一个文件:

 var jade = require('jade'), fs = require('fs'), path = require('path'); var fn = jade.compile(fs.readFileSync('index.jade', 'utf-8'), { filename: path.join(__dirname, 'index.jade'), pretty: true }); console.log(fn()); 

或者在“绝对path”中,在Jade中绝对是参照给定的基础代码,你会做以下的事情。 index.jade文件现在包含相同的文件,但使用绝对path,即。 include /foo

 var jade = require('jade'), fs = require('fs'); var fn = jade.compile(fs.readFileSync('index.jade', 'utf-8'), { basedir: __dirname, pretty: true }); console.log(fn()); 

有关所有选项,请参阅API文档 。

您可以使用语言环境中的函数来呈现dynamic模板。 这种方法也支持filter。

Jade文件

 +render('contents/index.md', {filter:'marked'}); 

吞咽pipe

 .pipe(jade({ locals: { render: function (filePath, options) { return jade.render(`include${options.filter ? (':' + options.filter) : ''} ${filePath}`, { filename: pathToJadeFileDir + '/something-not_important' }); } } }))