mozilla / nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)
https://mozilla.github.io/nunjucks/
BSD 2-Clause "Simplified" License
8.48k stars 634 forks source link

Why can't I include SVG from assets directory? #1462

Closed timokleemann closed 3 months ago

timokleemann commented 3 months ago

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?

devoidfury commented 3 months ago

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" %}

timokleemann commented 3 months ago

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'));
}