Is there a global object that is accessible from within a jade template with various parameters?
Is there a global variable with the path of the currently executing jade file?
!!! 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}
If you're using gulp-jade
, add gulp-data
to the mix and use this code:
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())
This will give you a relativePath
in your jade
templates that is something like about/index.jade
, relative to the base folder.
I'm not entirely sure of where/how that file.history
is generated, but in my case [0]
pointed to the original filename (with its absolute path on the disk)
My solution:
//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)
}
}));
}
In Jade file you can use _path_ and _basename_ like this:
<!-- #{_path_}, #{_basename_} -->
One more thing to notice: String interpolation does not work in jade comment. So following code will not interpolated in result html file:
// #{path}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With