pug - 我如何获取正在执行的 jade 脚本的文件名

标签 pug

是否有一个全局对象可以从带有各种参数的 jade 模板中访问?

当前执行的 Jade 文件的路径是否有全局变量?

!!! 5
html
  head
    title Test
  body
    //- I want to be able to know what the current script file is...
    p Hello, I am: #{globals.scriptfilename}

最佳答案

如果您使用的是 gulp-jade , 添加 gulp-data混合并使用此代码:

var jade = require('gulp-jade');
var data = require('gulp-data');

gulp.src('**/*.jade')
    .pipe(data(function (file) {
        return {
            relativePath: file.history[0].replace(file.base, '')
        };
    }))
    .pipe(jade())

这会给你一个 relativePath在您的 jade类似于 about/index.jade 的模板, 相对于基本文件夹。

我不完全确定在哪里/如何file.history已生成,但在我的情况下 [0]指向原始文件名(及其在磁盘上的绝对路径)

关于pug - 我如何获取正在执行的 jade 脚本的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12262844/

相关文章:

javascript - 如何使用Pug将一个目录下的Markdown文件转成HTML页面?

node.js - Jade : Why && is compiled to & when used in angularjs in Jade template?

javascript - 使用 pug-html-loader 将数据传递给哈巴狗(无法读取未定义的属性)

javascript - 从一个函数内部的 request.get 返回变量值到另一个 NODEJS 表达式

javascript - ExpressJS - 我的 Jade 布局中的编译错误较少

node.js - Jade 模板引擎中的 IE 条件语句

node.js - 哈巴狗( Jade )模板 : automatically update references to scripts and stylesheets with fingerprinted version in CDN

javascript - 广播到房间的套接字 IO 无法正常工作

javascript - Pug - 从 JSON 对象检索 key

css - 内联 CSS 无法使用 jade