Giter VIP home page Giter VIP logo

Comments (4)

Richie765 avatar Richie765 commented on May 30, 2024

I too was trying to include a file in many different ways and all of them failed. For instance:

{% set standardModal %}
    {% include 'standardModalData.html' %}
{% endset %}

Only an include outside any block seems to work.

from express-nunjucks.

pkolt avatar pkolt commented on May 30, 2024

It seems that this is a bug of nunjucks.
To solve you can use the synchronous loader templates - nunjucks.FileSystemLoader.

const express = require('express');
const expressNunjucks = require('express-nunjucks');

const app = express();

app.set('views', __dirname + '/templates');

const njk = expressNunjucks(app, {
    loader: nunjucks.FileSystemLoader
});

app.get('/', (req, res) => {
    res.render('index');
});

app.listen(3000);

from express-nunjucks.

davethegr8 avatar davethegr8 commented on May 30, 2024

This also seems to happen while inside loops.

before {% include "partials/hello-world.html" %} after

{% set items = [1, 2, 3] %}
{% for i in items %}
- {{ i }} before {% include "partials/hello-world.html" %} after
{% endfor %}

Output:

before Hello World after

- 1 before 
- 2 before 
- 3 before 

Interestingly, besides using the FileSystemLoader, setting noCache: true in the configure also works around this issue.

from express-nunjucks.

gijswijs avatar gijswijs commented on May 30, 2024

You can not use includes inside for-loops if you use async loaders. There are two alternative tags called asyncEach and asyncAll that work like the for-loop but allow for async loaders. See the documentation here:
https://mozilla.github.io/nunjucks/templating.html#asynceach

Be aware that macro's don't allow for any async stuff happening inside the macro. So if you use async loaders, includes aren't an option to use inside macro's. A dirty workaround for that is to have the include before the macro call. Example:

<div>
{% include "partials/icons/" + platform + ".svg" %}
{{ icon.icon(platform) }}
</div>

from express-nunjucks.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.