How to use nunjucks

Comprehensive nunjucks code examples:

How to use nunjucks.precompile:

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

async precompile() {
	return new Promise((resolve, reject) => {
		const templates = nunjucks.precompile(
			path.join(__dirname, '../_includes/'),
			{
				include: [
					'preview.njk',

How to use nunjucks.Template:

30
31
32
33
34
35
36
37
38
39
40
41
  edge: 'Edge',
  safari: 'Safari',
};
const DATA = bcd();


const TEMPLATE = new Nunjucks.Template(
  fs.readFileSync(path.join(__dirname, 'template.njk'), {encoding: 'utf-8'})
);


/**

How to use nunjucks.FileSystemLoader:

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

How to use nunjucks.runtime:

121
122
123
124
125
126
127
128
129
130
}

let source = src

if (!source || source.trim().length === 0) {
  source = new nunjucks.runtime.SafeString((
    vars.isMarkdown ? `![](${result})` : `
      <img ${options.class ? `class="${options.class}"` : ''} src="${result}"
        width="${chartOptions.width}"
        height="${chartOptions.height}"

How to use nunjucks.Environment:

0
1
2
3
4
5
6
7
8
9
10
const nunjucks = require('nunjucks')
const fs = require('fs')


let filenames = ['body-ch', 'body-en']
filenames.forEach(filename => {
    let env = new nunjucks.Environment();
    let precompiledBody = nunjucks.precompile(filename + '.njk', {
        env: env
    })
    fs.writeFile((filename + ".js"), precompiledBody, () => {

How to use nunjucks.compile:

55
56
57
58
59
60
61
62
63
        })
      }
    }

    const env = new Nunjucks.Environment(loader, options.compile)
    const template = Nunjucks.compile(doc.body.data.toString(), env, doc.file.path)
    return Promise.resolve(template)
  }
}

How to use nunjucks.renderString:

152
153
154
155
156
157
158
159
160
161
    this.innerHTML = this.template('Ricci')
    this.get('/customer/data').then(d=>{
        const template = $('#customer-data').textContent;
        console.log(template)
        console.log(d)
        this.innerHTML = nunjucks.renderString(template, d);
    });
}

template(name) {

How to use nunjucks.render:

7
8
9
10
11
12
13
14
15
16
17


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('<')
    })
  })

How to use nunjucks.configure:

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('<')
    })
  })