mozilla/nunjucks

Why can't I include SVG from assets directory?

timokleemann opened this issue · 2 comments

Hi,

Is there a reason why this is giving me a template not found error?

<header>
	<a id="logo" href="/">
		{% include "./../assets/images/logo.svg" %}
	</a>
</header>

If I put the logo.svg in the same directory as my header.njk partial, the SVG gets loaded without any problems:

<header>
	<a id="logo" href="/">
		{% include "./logo.svg" %}
	</a>
</header>

I would like to store the SVG in my images folder, though, not in my partials folder. How can this be done?

Every path you reference via include or via .render calls must be within the search paths you configure for the engine.

Somewhere you may have a line like, nunjucks.configure('partials') to set the view directory; change it to nunjucks.configure(['./partials', './images']) and then include the path relative to that; eg {% include "./logo.svg" %}

Thanks. I managed to get it to work by changing my Gulpfile accordingly:

const { src, dest, series, watch } = require('gulp');
const njk = require('gulp-nunjucks-render');

function createHTML() {
  return src('src/html/pages/*.+(html|njk)')
    .pipe(njk({ path: ['src/html', 'src/images'] })) // <-- Added a second path to the array here!
    .pipe(dest('dist'));
}