gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 04:36:52 25 4
gpt4 key购买 nike

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

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

有两个文件:

索引. 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

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

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

基本路径的代码如下:

.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

我完全不知所措。我在某处缺少其他设置吗?感觉这应该是一件 super 简单的事情。我错过了什么?

最佳答案

您正在使用 jade.compile(),对吧?如错误消息所述,您没有传递 filename 选项。因为你只是给了一个字符串来编译成 Jade,所以它不知道去哪里寻找包含的文件。

脚本和 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());

或者使用 Jade 中相对于给定 basedir 的绝对路径,您可以执行以下操作。 index.jade 文件现在包含相同的文件,但使用绝对路径,即。 包含/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 docs .

关于javascript - 无法使用 Jade 模板包含相对路径文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28038630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com