How to use the configure function from nunjucks

Find comprehensive JavaScript nunjucks.configure code examples handpicked from public code repositorys.

nunjucks.configure is a function in the Nunjucks library that sets the configuration options for rendering templates.

6
7
8
9
10
11
12
13
14
15
16
const { renderTemplate } = require('../../lib/jest-helpers')


describe('Template', () => {
  describe('with default nunjucks configuration', () => {
    it('should not have any whitespace before the doctype', () => {
      nunjucks.configure(join(paths.src, 'govuk'))
      const output = nunjucks.render('./template.njk')
      expect(output.charAt(0)).toEqual('<')
    })
  })
fork icon286
star icon844
watch icon60

+ 15 other calls in file

146
147
148
149
150
151
152
153
154
155
156
const Cookies = require("js-cookie");
const nunjucks = require("nunjucks");


class CustomerDataPage extends HTMLElement {
    connectedCallback() {
        nunjucks.configure({ autoescape: true });
        this.innerHTML = this.template('Ricci')
        this.get('/customer/data').then(d=>{
            const template = $('#customer-data').textContent;
            console.log(template)
fork icon6
star icon2
watch icon2

+ 2 other calls in file

How does nunjucks.configure work?

nunjucks.configure is a method in the Nunjucks templating engine that sets up the environment with the specified options, such as the location of the templates, the syntax, and custom filters and functions. It takes an object as an argument, which can have properties such as autoescape, tags, watch, and filters, among others. Once configured, the environment can be used to render templates.

164
165
166
167
168
169
170
171
172
173
	),
	pathNormalizer(siteConfiguration.dir.input),
	normalize(path.normalize(".")),
];
console.log("nunjucksFileSystem", nunjucksFileSystem);
let nunjucksEnvironment = Nunjucks.configure(
	new Nunjucks.FileSystemLoader(nunjucksFileSystem),
	{
		throwOnUndefined: throwOnUndefinedSetting,
		autoescape: true,
fork icon3
star icon5
watch icon1

+ 2 other calls in file

13
14
15
16
17
18
19
20
21
22
 * hexo-renderer-nunjucks
 * @param {import('hexo')} hexo
 */
function rendererNunjucks(hexo) {
  const themeDir = path.join(__dirname, '../themes', hexo.config.theme);
  const env = nunjucks.configure([themeDir, path.join(themeDir, 'layout')], {
    noCache: true,
    autoescape: false,
    throwOnUndefined: false,
    trimBlocks: false,
fork icon1
star icon2
watch icon1

+ 15 other calls in file

Ai Example

1
2
3
4
5
6
7
const nunjucks = require("nunjucks");

// Set up the views directory and the templating engine
nunjucks.configure("views", {
  autoescape: true,
  express: app,
});

In this example, nunjucks.configure() is used to set up the views directory and the templating engine for an Express app. The first argument is the path to the views directory, and the second argument is an object containing configuration options. In this case, autoescape is set to true, which means that all output will be automatically escaped to prevent XSS attacks. The express option is also set to app, which is the instance of the Express app that will be using Nunjucks as its templating engine.

11
12
13
14
15
16
17
18
19
20
 * hexo-renderer-nunjucks
 * @param {import('hexo')} hexo
 */
function rendererNunjucks(hexo) {
    var themeDir = path.join(__dirname, '../themes', hexo.config.theme);
    var env = nunjucks.configure([themeDir, path.join(themeDir, 'layout')], {
        noCache: true,
        autoescape: false,
        throwOnUndefined: false,
        trimBlocks: false,
fork icon1
star icon2
watch icon1

+ 14 other calls in file

175
176
177
178
179
180
181
182
183
184
  watch: NODE_ENV !== 'production', // Reload templates when they are changed (server-side). To use watch, make sure optional dependency chokidar is installed
  noCache: NODE_ENV !== 'production', // Never use a cache and recompile templates each time (server-side)
}

// Initialise nunjucks environment
const nunjucksEnvironment = configure(APP_VIEWS, nunjucksConfiguration)

// add custom date formatters
nunjucksEnvironment.addFilter('date', dateFilter)
nunjucksEnvironment.addFilter('mojDate', mojDate)
fork icon2
star icon0
watch icon11

+ 4 other calls in file

112
113
114
115
116
117
118
119
120
121
122
123


// Set EJS as templating engine
app.set('view engine', 'ejs');


// Configure nunjucks template engine
const env = nunjucks.configure('viewsNunjucks', {
	autoescape: true,
	express: app
});

fork icon0
star icon0
watch icon2

8
9
10
11
12
13
14
15
16
17
18
    }
  };


function njkCompile(data) {
  const templateDir = path.dirname(data.path);
  const env = nunjucks.configure(templateDir, {
    autoescape: false,
    throwOnUndefined: false,
    trimBlocks: false,
    lstripBlocks: false
fork icon0
star icon0
watch icon1

+ 5 other calls in file

61
62
63
64
65
66
67
68
69
70
	}
});
// Initialize express
const app = express();
// Configure nunjucks template engine
const env = nunjucks.configure('views', {
  	autoescape: true,
  	express: app
});
env.addFilter('formatNumber', num => String(num).replace(/(.)(?=(\d{3})+$)/g,'$1,'));
fork icon0
star icon0
watch icon1

+ 13 other calls in file

-2
fork icon0
star icon0
watch icon0

+ 8 other calls in file

46
47
48
49
50
51
52
53
54
55
56
function njkCompile(data) {
  let env;
  if (data.path) {
    env = nunjucks.configure(dirname(data.path), nunjucksCfg);
  } else {
    env = nunjucks.configure(nunjucksCfg);
  }
  nunjucksAddFilter(env);


  const text = 'text' in data ? data.text : readFileSync(data.path);
fork icon0
star icon0
watch icon1

+ 7 other calls in file

108
109
110
111
112
113
114
115
116
117
static set_app_element(selector) {
    View.get().app_element = document.querySelector(selector);
}
static get() {
    if (View.instance == undefined || View.instance == null) {
        nunjcks.configure("./../templates", { autoescape: false });
        View.instance = new View();
    }
    return View.instance;
}
fork icon0
star icon0
watch icon1

415
416
417
418
419
420
421
422
423
424
class View {
    constructor() { }
    static get() {
        if (View.instance == undefined || View.instance == null) {
            View.instance = new View;
            nunjucks.configure("../Templates", { autoescape: false });
        }
        return View.instance;
    }
    get_app_element(selector) {
fork icon0
star icon0
watch icon0

205
206
207
208
209
210
211
212
213
214
    logger.info(`question type ${token.qtype} not match!`)
    return ''
  }
}
const { configure } = require('nunjucks')
let env = configure(path.join(__dirname, '../templates/snipets/qas/'))
env.addFilter('trans', trans)

var res = env.render(template, {
  qa: token
fork icon0
star icon0
watch icon0