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

eri*_*sch 7 javascript node.js pug

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

the "filename" option is required to use "include" with "relative" paths 
Run Code Online (Sandbox Code Playgroud)

有两个文件:

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
Run Code Online (Sandbox Code Playgroud)

我尝试使用基本路径,但后来收到以下错误:

the "basedir" option is required to use "include" with "absolute" paths
Run Code Online (Sandbox Code Playgroud)

基本路径的代码如下:

.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
Run Code Online (Sandbox Code Playgroud)

我完全不知所措.我错过了哪个地方?感觉这应该是超级简单的东西.我错过了什么?

ves*_*sse 10

你在用jade.compile(),对吗?如错误消息所示,您没有传递该filename选项.由于您只提供了一个字符串来编译为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());
Run Code Online (Sandbox Code Playgroud)

或者使用"绝对路径",在Jade中绝对参考给定的,basedir您将执行以下操作.该index.jade文件现在包含相同的文件,但使用绝对路径,即.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());
Run Code Online (Sandbox Code Playgroud)

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