是否有可以从带有各种参数的翡翠模板中访问的全局对象?
当前执行的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}
是否有可以从带有各种参数的翡翠模板中访问的全局对象?
当前执行的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]
指向原始文件名(其磁盘上的绝对路径)
我的解决方案:
//gulpfile.js
var $path = require('path'),
jade = require('gulp-jade'),
isProduction = process.env.ENV == 'production';
gulp.task('watch', function() {
gulp.watch("**/*.jade")
.on('change', function(event) {
compileJade(event.path, isProduction);
});
});
function compileJade(path, isCompress) {
gulp.src(path)
.pipe(jade({
pretty: !isCompress,
locals: {
_path_: path,
_basename_: $path.basename(path)
}
}));
}
在 Jade 文件中,您可以像这样使用 _path_ 和 _basename_:
<!-- #{_path_}, #{_basename_} -->
还要注意的一件事:字符串插值在翡翠注释中不起作用。因此,以下代码不会插入结果 html 文件中:
// #{path}
有一个全局变量。您可以使用 Node 的util.inspect(object)来查看它的内容。
与JSON.stringify()
.
使用样板 Express 应用程序,我发现了这些:
['node','/Users/mike/Development/test/web.js']
'/Users/mike/Development/test/web.js'