question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nunjucks with multiple directories(for layouts, templates, partials) support.

See original GitHub issue

Hi,

I am doing following.

/*
config._views is my views directory
  ~/nunjucks-demo/views
         --views
             -templates
             -layouts
             -partials

 engines => consolidate
 module => "nunjucks"
*/


// express cofiguration
app.engine(ext, engines[module]);
app.set('view engine', ext);
app.set('views', path.join(config._views, 'templates'));

// nunjucks configuration
var nunjucks = require('nunjucks');
var env = nunjucks.configure(config._views, {
            autoescape: false,
            express: app
        });

// nunjucks-demo/views/templates/index.html
{% extends "layout/default.html" %}
{% block content %}
    <section>
        <h1>{{entry.title}}</h1>
        <ul>
            <li><b>markdown:</b> {{ entry.markdown }}</li>
        </ul>
    </section>
{% endblock %}

// nunjucks-demo/views/layout/default.html
<!DOCTYPE HTML>
<html>
    <head>
        <title>{{entry.title}}</title>
        <link type="text/css" rel="stylesheet" href="/static/css/style.css">
        <script type="text/javascript" src="/static/js/script.js"></script>
    </head>
    <body>
        {% include "partials/header.html" %}
        {% block content %}{% endblock %}
        {% include "partials/footer.html" %}
    </body>
</html>

But it doesn’t work I have to make all the templates and layouts in single directory.

Layouts are not even extending. This same thing works in swig.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jlongstercommented, Aug 28, 2015

You can pass multiple directories to nunjucks.configure to load from. So: nunjucks.configure(['views', 'views/templates', {}).

Your question is not entirely clear. If you could explain the problem and show the error you’re getting that would help.

1reaction
mcornellacommented, Jan 15, 2016

I think what @hiteshbalraw means is to be able to pass a partials option that will set the root folder of where to look for the nunjucks templates.

For instance, if we have the following structure:

views
├── layouts
├── partials
└── templates

and we pass { partials: 'views' } in the context of render and renderString, then we don’t need to specify the whole path to include/extend a file, but only the relative path to the root folder (in this case, views): views/layouts/default.html turns to layouts/default.html. This is, essentially, an alternative to having to use nunjucks.configure(path).

This is used a lot by consolidate.js, and I need this one specifically to use it with the metalsmith-in-place plugin. If you need more context, here’s how consolidate.js calls the nunjucks renderString method.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nunjucks extends directive not working with template in ...
I guess extends work only if the template is in same directory and not in separate directory also i tried to specify multiple...
Read more >
nunjucks templating docs
Macros and top-level assignments (done with set ) are exported from templates, allowing you to access them in a different template. Imported templates...
Read more >
Template Language—Nunjucks — Eleventy
base.njk' %} looks for base.njk in the template's current directory. ... Nunjucks supports some asynchronous behavior, like filters. Here's how that works:
Read more >
How to Modularize HTML Using Template Engines and Gulp
The templates folder is used for storing all Nunjucks partials and other Nunjucks files that will be added to files in the pages...
Read more >
Nunjucks: A JavaScript Template Engine | by Andy Neale
Templating frameworks separate the layout of dynamic web sites and ... Poor support, however, for partials and blocks and other elements ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found