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
5
votes
2 answers

How do access NODE_ENV in Nunjucks template?

I have some Nunjucks template blocks that I want to render only in certain environments. I can't seem to access the NODE_ENV variable though. I tried this: {% if process.env.NODE_ENV === 'development' %}
rendering some stuff here
{%…
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
5
votes
0 answers

Is there a solution to converting a nunjucks .njk file to a twig php file?

Currently trying to find/build a process that allows me to take .njk files from USWDS and convert them into php twig files for a Drupal 8 project. (Now, at this current point in time they are similar. So with a little tweaking you could easily make…
5
votes
1 answer

How to check whether the substring is present in a string in Nunjucks Template

If we have a string like "This is High Price" how can I check whether "high" is present in that string or not in Nunjucks Template
himabindu
  • 336
  • 3
  • 15
5
votes
2 answers

Using nunjucks with htmlWebpackPlugin using dynamic vars in the template

I'm searching a way of using Nunjucks with the htmlWebpackPlugin to generate some html files on webpack compiling. What I achieved so far I managed to actually generate HTML from nunjucks template files through the nunjucks-html-loader but looking a…
3Dos
  • 3,210
  • 3
  • 24
  • 37
5
votes
2 answers

Nunjucks: Loop through first 5 items in for loop

New to Nunjucks and have a similar problem to here but I couldn't get my code to work. I'm trying to loop through the first 5 items in a Nunjucks loop of 14 items. So far I've found that the range function should be able to accomplish this but can't…
Yawner
  • 93
  • 2
  • 10
5
votes
4 answers

Error: callback function required

I am trying to render an object into html using node,express and mongodb. when I tried the below code var express=require('express'), app=express(), engines = require('consolidate'), nunjucks = require('nunjucks'); app.set('view engine',…
maverick
  • 73
  • 1
  • 8
4
votes
1 answer

Several CSS files for 11ty with Nunjucks

So I have an 11ty site structured like this . └── app/ ├── public ├── src / │ ├── _includes/ │ │ ├── base.njk │ │ ├── header.njk │ │ └── footer.njk │ ├── css/ │ │ ├── about.css │ │ ├──…
Mohammed Asker
  • 97
  • 1
  • 1
  • 14
4
votes
1 answer

How to list tags for the current post - Eleventy js?

I am using Eleventy JS. And I can't list the tags for the current post. There is nothing written about this in the official documentation. All tags are listed easily. But for the current post I can not display the tags on the screen This code to…
4
votes
1 answer

how to use if condition to include in nunjucks?

so I have a page parameter sent from server.So I set the currentPage to be event if it is event otherwise default. I'm trying to include only when page is default. The following code works when current page is not event i.e default but for event…
4
votes
1 answer

Use variable and filter within tag in nunjucks + eleventy

I am a nunjucks newbie - trying it out along with eleventy. Short version: can I use variables and filters within tags in nunjucks? For example: {% set myVar = {{ title | lower }} %} (assume the {{ title }} variable is set correctly) Longer version…
4
votes
3 answers

Get current URL in Nunjucks template?

Seems like it should be pretty straightforward, but I'm having a really hard time finding an answer. How do you find the current page's URL within a Nunjucks template? Something like this would be perfect: Some link
saricden
  • 2,084
  • 4
  • 29
  • 40
4
votes
4 answers

How can I group JSON data and count by month element using Nunjucks or Node.JS

Given this data as an example, this is just 2 records, there's hundreds over several months { "responses": [{ "responseid": 1, "q1": 1, "q2": 1, "q3": 1, "q4": 1, "q5": 2, "response": "Response 1 example feedback", …
Codesight
  • 317
  • 2
  • 14
4
votes
0 answers

Detect changes between multiple source files using Gulp

The original issue I was having was that the task was running x amount of times for the number of files existing inside src.pages, I fixed this by adding the 'changed-in-place' plugin, to compare the last date modified of the source files. This…
Luke
  • 195
  • 2
  • 16
4
votes
1 answer

Is there any way to get a substring from a string in Nunjucks templating language?

If we have value 'cats', I want the substring 'at' (need to delete first and last letters). How can we accomplish that in a Nunjucks template?
himabindu
  • 336
  • 3
  • 15
4
votes
1 answer

How can you pass JSON data into a Nunjucks file?

I have a .njk file that I would like to populate with data from a JSON file. Currently I am using Webpack. Here is my JSON File: { "ranking": "Colors", "description": "Here is a ranking of my favorite colors", "rankings": [ { …
flatafor
  • 57
  • 1
  • 2
1 2
3
33 34