Closed slavafomin closed 7 years ago
+1
Right now, gulp-nunjucks uses the base path of the file that was passed to the plugin as the first argument that is passed to the nunjucks.FilesystemLoader constructor. This means that it is impossible to for instance include any files that live in any parent directory.
Maybe allow specifying paths
in the compile signature?
@slavafomin You can work around it by passing a nunjucks.Environment in the options to compile like so:
.pipe(gnunjucks.compile({},{
env: new nunjucks.Environment(new nunjucks.FileSystemLoader('some-base-path'))
}))
This base path will be used for all your require and extend statements.
Yup, that's the correct way to do it, else it falls back to file.base
. https://github.com/sindresorhus/gulp-nunjucks/blob/master/index.js#L21.
Thanks @wolthers, for a suggestion!
I've found this module to have better API and more options: https://github.com/carlosl/gulp-nunjucks-render
And it has path
option out of the box. Gonna use it for now.
A slightly cleaner way around this can be achieved by using gulp-base:
.pipe(base('src'))
.pipe(nunjucks.compile())
.pipe(base('src/html'))
If you get
TypeError: nunjucks.FileSystemLoader is not a constructor
That is probably because you imported gulp-nunjucks
as nunjucks
, like
const nunjucks = require('gulp-nunjucks');
You need to import the original nunjucks
like this:
const nunjucks_lib = require('nunjucks');
And usage could be like
gulp.src('some-base-path/file.template')
.pipe(nunjucks.compile(data,{
env: new nunjucks_lib.Environment(new nunjucks_lib.FileSystemLoader('some-base-path'))
}))
// ...
After around 5 years I've stumbled upon the same problem and had to use another package again :)
By the way, I still don't understand why this issue was closed.
After so many hours of investigation I've found out that all I had to do was add a second argument to the src
function of gulp
:
gulp.src('./docs/homepage/index.html', {base: './docs'})
Because gulp-nunjucks
will fallback to this path, and so the ./docs
will be the base path and all include
statements in the Nunjucks code needs to be changed now to take this into consideration.
then I was able to include files in my templates which where outside the ./docs/homepage
folder but still inside its parent folder.
Hello!
Thank you for this great library!
However, how do I specify the base path for templates? The
opts
here doesn't have such option.Thanks!