ianyoung
ianyoung

Reputation: 3035

Is there a common convention for template extensions when using Nunjucks?

When working with Nunjucks templates which requite rendering or compilation is there a standard naming convention to be used to have them processed? i.e. file.nunjucks, file.nunjucks.html, or file.njs etc.

I know that when working with other template languages it's common to use their name as the extension such as file.liquid, file.ejs, etc, but I've not seen much in reference to Nunjucks.

Upvotes: 16

Views: 5480

Answers (2)

Macleod Sawyer
Macleod Sawyer

Reputation: 320

I personally prefer the extension '.njk', its also something that they have as an example on the Nunjucks docs.

File Extensions

Although you are free to use any file extension you wish for your Nunjucks template files, the Nunjucks community has adopted .njk.

If you are developing tools or editor syntax helpers for Nunjucks, please include recognition of the .njk extension.

Upvotes: 22

xmojmr
xmojmr

Reputation: 8145

Jon Buckley's nunjucks plugin for wintersmith supports template naming convention

*.html
*.nunjucks

See https://github.com/jbuck/wintersmith-nunjucks/issues/8 for proof

So this naming convention is common everywhere wintersmith site generator is used. Especially the *.html seems to be fairly common also elsewhere.

Nunjucks's own documentation uses the *.html in examples of using {% include ..%} and {% extends ..%} tags and it says

...overview of the templating features available in nunjucks. Nunjucks is essentially a port of jinja2, so you can read their docs if you find anything lacking here...

and jinja's own documentation in turn says

...A template is simply a text file. It can generate any text-based format (HTML, XML, CSV, LaTeX, etc.). It doesn’t have a specific extension, .html or .xml are just fine...

My in-house site generator applies the nunjucks preprocessor also to files with extensions: *.md, *.markdown, *.htm, *.html, *.php, *.css, *.js, .htaccess but it can not be considered "common convention".

It might be possible to find out nunjuck's usage statistics and examples of used naming conventions using Google or GitHub or Wolfram Alpha computational knowledge engine or IBM Watson Analytics service...


I think that you can use any naming convention as long as you are able to refactor (rename) it anytime later

Upvotes: 7

Related Questions