Nunjucks with multiple directories(for layouts, templates, partials) support.
See original GitHub issueHi,
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:
- Created 8 years ago
- Comments:11 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
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:
and we pass
{ partials: 'views' }
in the context ofrender
andrenderString
, 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 tolayouts/default.html
. This is, essentially, an alternative to having to usenunjucks.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.