Nunjucks: How to use relative file paths in html files?

Viewed 1302

If I use Flask and Jinja2, I can do something like this:

<link href="{{ url_for('static', filename='css/bootstrap.css') }}" rel="stylesheet" type="text/css"/>
<script src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script>

How do I do something like this with Nunjucks and ExpressJS ? Or lets say what is the standard approach in Nunjucks to tell the index.html which folders it should use to look for css, js & img files?

1 Answers

In your main file, declare the folder to serve static files.

app.use(express.static(path.join(__dirname, 'public')))

Now in your template file, if you refer to

<img src='/logo.png'>

The file /public/logo.png will be loaded

Related