Questions tagged [nunjucks]

A rich and powerful templating language for JavaScript

Nunjucks is a port of jinja2.

Tag questions with Nunjucks if you have issues with any of the following functionalities in the templates:

  • Variables
  • Filters
  • Template Inheritance
  • Tags if for asyncEach1 asyncAll macro set extends block include import raw
  • Keyword Arguments
  • Comments
  • Whitespace Control
  • Expressions Math Comparisons Logic`` If Expression Function Calls`
  • Autoescaping
  • Global Functions range([start], stop, [step]) cycler(item1, item2, ...itemN) joiner([separator])
  • Builtin Filters

official website

510 questions
178
votes
3 answers

Convert integer to string Jinja

I have an integer {% set curYear = 2013 %} In {% if %} statement I have to compare it with some string. I can't set curYear to string at the beginning because I have to decrement it in loop. How can I convert it?
Boris Zagoruiko
  • 12,705
  • 15
  • 47
  • 79
47
votes
2 answers

Integrating Nunjucks with IntelliJ IDEA / Webstorm

How can I add syntax highlighting to Nunjucks template files in IntelliJ IDEA / Webstorm? There is no obvious plugin available. Perhaps there is something for a similar templating engine that would work?
JoshuaDavid
  • 8,861
  • 8
  • 47
  • 55
38
votes
1 answer

Nunjucks: 'if' with multiple 'and' or 'or' condition

Today my team mate was struggling on how to add multiple conditions with 'and' or 'or' in an if statement in Nunjucks template. After a lot of search he found the answer but not on Stackoverflow. I am not sure if the answer is already posted…
akhileshnair
  • 1,437
  • 1
  • 12
  • 18
26
votes
3 answers

Stop nunjucks from escaping HTML

I have a comments AJAX call which returns data of the posted comment, I also have @mention functionality built in, the server side is processing the @mentions and doing a str_replace on the mentioned users replacing their names with an a tag within…
André Figueira
  • 6,048
  • 14
  • 48
  • 62
25
votes
1 answer

Loop by integer value with Nunjucks Templating

I'm quite new to nunjucks and from what I have read this is not possible, but I was wondering if anyone had come up with a way of doing this. I am basically looking to perform a for loop in a nunjucks template based on a value rather than an…
Jamie Bradley
  • 629
  • 2
  • 7
  • 18
18
votes
4 answers

How can I pass JSON data into a Nunjucks template?

I want to use Nunjucks templates but want to pass in my own JSON data to be used on the templates. The documentation here is pretty sparse. https://mozilla.github.io/nunjucks/templating.html Thank you.
sjmartin
  • 3,912
  • 4
  • 19
  • 32
18
votes
3 answers

Loop through object properties nunjucks

I've got following model: items: { someId1: { property1.... }, someId2: {...}, someIdN: {...} } I would like to get a for-loop in my template (nunjucks) which goes through all the "someId's". Does anyone…
Tikkes
  • 4,599
  • 4
  • 36
  • 62
17
votes
2 answers

Simplest possible node.js + nunjucks example

Probably will never use node.js nor Nunjucks for any real development, but now for some reason need: precompile the some simple templates to javascript with Nunjucks run the precompiled templates under the node.js I have done: installed node.js…
kobame
  • 5,766
  • 3
  • 31
  • 62
15
votes
2 answers

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…
ianyoung
  • 2,863
  • 4
  • 26
  • 31
12
votes
2 answers

Nunjucks nl2br does not exist?

I need a filter like the Jinja "nl2br", but in the Nunjucks. In the documentation are a mention (https://mozilla.github.io/nunjucks/templating.html), but I searched it in the nunjucks code…
12
votes
2 answers

if... !true conditional rendering in nunjucks

if...true conditionals work like a charm as outlined here in the docs. but if I try to do something like: {% if !posts.length %} No project posts yet! {% endif %} I get an error: Template render error:…
nak
  • 3,077
  • 3
  • 27
  • 35
11
votes
3 answers

How can I sort by date with Nunjucks?

I'm trying to use the jinja documentation to figure it out but all my attempts are failing. http://jinja.pocoo.org/docs/dev/templates/#sort Here is some test JSON data: items: [{ name: 'item 1', time: '2015-02-12T00:38:18.055Z' …
sjmartin
  • 3,912
  • 4
  • 19
  • 32
11
votes
3 answers

How can I define global variables in nunjucks?

Using nunjucks, how can I define some global variables that should always be available within all templates? Ideally, they would be specified somewhere in the environment or config options and not have to be merged into the context dict with each…
mpen
  • 272,448
  • 266
  • 850
  • 1,236
10
votes
2 answers

Check if key exists in array in nunjucks template (Node JS)

I am creating an application in Node Js using Nunjucks template engine and I have to apply permissions on pages to show add, edit and delete links. For this I have implemented an array of permissions like below : var user_params = ['add_user',…
Ankit
  • 627
  • 1
  • 9
  • 22
10
votes
1 answer

Check if an object is empty in Nunjucks

So I'm using Nunjucks as the templating engine in my Node.js application. I have an object we'll call var which may or may not be empty. When it is empty, if I do {{ var | dump }} Nunjucks properly shows that it is an empty object, displaying…
Mogzol
  • 1,405
  • 1
  • 12
  • 18
1
2 3
33 34