Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I specify the base path for templates? #14

Closed
slavafomin opened this issue Aug 25, 2016 · 8 comments
Closed

How do I specify the base path for templates? #14

slavafomin opened this issue Aug 25, 2016 · 8 comments

Comments

@slavafomin
Copy link

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!

@wolthers
Copy link

wolthers commented Sep 7, 2016

+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?

@wolthers
Copy link

wolthers commented Sep 7, 2016

@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.

@kevva
Copy link

kevva commented Sep 8, 2016

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.

@kevva kevva closed this as completed Sep 8, 2016
@slavafomin
Copy link
Author

slavafomin commented Sep 8, 2016

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.

@kasperisager
Copy link

A slightly cleaner way around this can be achieved by using gulp-base:

.pipe(base('src'))
.pipe(nunjucks.compile())
.pipe(base('src/html'))

@luckydonald
Copy link

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'))
  }))
  // ...

@slavafomin
Copy link
Author

slavafomin commented Sep 24, 2021

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.

@yairEO
Copy link

yairEO commented Apr 28, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants