How to use the Environment function from nunjucks

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

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, () => {
fork icon2
star icon13
watch icon0

7
8
9
10
11
12
13
14
15
16
17
const nunjucks = require('nunjucks');
const postcssPresetEnv = require('postcss-preset-env');
const highlight = require('highlight.js');
const prettier = require('prettier');


const nunjucksEnvironment = new nunjucks.Environment(
  new nunjucks.FileSystemLoader(path.resolve(__dirname, 'src', 'docs')),
  {
    throwOnUndefined : true,
  },
fork icon3
star icon3
watch icon1

+ 4 other calls in file

12
13
14
15
16
17
18
19
20
21
22
        .filter(x => x)
        .join('/')
}


function createEnvironment() {
    const environment = new nunjucks.Environment(
        new nunjucks.FileSystemLoader(
            path.resolve(__dirname, '..'),
            {
                throwOnUndefined: true,
fork icon3
star icon3
watch icon2

+ 3 other calls in file

54
55
56
57
58
59
60
61
62
63
          cb(`Failed to load layout $(path}`)
        })
      }
    }

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

373
374
375
376
377
378
379
380
381
382
 * Env Nunjucks
 * @param {import('express').Express} app
 * @returns
 */
function envNunjucks(app) {
	const env = new nunjucks.Environment([new nunjucks.FileSystemLoader(__dirname)]);
	if (typeof app === 'object') {
		try {
			nunjucks.configure(__dirname, {
				autoescape: true,
fork icon0
star icon2
watch icon0

24
25
26
27
28
29
30
31
32
33
  return null;
},

async load(id) {
  const env =
    typeof envFn === "function" ? await envFn(new Environment()) : envFn;

  if (id.endsWith(".nunjucks.precompiled.js")) {
    // Generate the precompiled template module based on the ID
    const templatePath = id.slice(0, -".nunjucks.precompiled.js".length);
fork icon0
star icon1
watch icon0

3
4
5
6
7
8
9
10
11
12
var
    autoescape = opts.autoescape === undefined ? true : opts.autoescape,
    noCache = opts.noCache || false,
    watch = opts.watch || false,
    throwOnUndefined = opts.throwOnUndefined || false,
    env = new nunjucks.Environment(
        new nunjucks.FileSystemLoader(path || 'views', {
            noCache: noCache,
            watch: watch,
        }), {
fork icon0
star icon0
watch icon2

+ 3 other calls in file

160
161
162
163
164
165
166
167
168
169
}

delete require.cache[require.resolve(path.normalize(`${baseDataLocal}/global.PROD.js`))];
const dataGlobal          = await GetData(`global.${ENV}`, baseDataLocal, baseDataRemote, baseDataRemoteCache);

const nunjucksEnv         = new nunjucks.Environment(new nunjucks.FileSystemLoader(dir.src));
nunjucksEnv.addExtension('ComponentExtension', new ComponentExtension(nunjucks, nunjucksEnv));

const gulpData = async (file) => {
    if (typeof file.data !== 'undefined') return file.data;
fork icon0
star icon0
watch icon1

14
15
16
17
18
19
20
21
22
23
	return;
}

const context = Object.assign({}, data, file.data);
const filePath = file.path;
const env = options.env || new nunjucks.Environment(new nunjucks.FileSystemLoader(file.base), options);

if (options.filters && !options.env) {
	for (const key of Object.keys(options.filters)) {
		env.addFilter(key, options.filters[key]);
fork icon0
star icon0
watch icon0

4
5
6
7
8
9
10
11
12
13
14
const nunjucks = require('nunjucks');
const { inherits } = require('util');
const Promise = require('bluebird');


function Tag() {
  this.env = new nunjucks.Environment(null, {
    autoescape: false
  });
}

fork icon0
star icon0
watch icon0

+ 2 other calls in file

13
14
15
16
17
18
19
20
21
22
}

setLibrary(env) {
  this.njkEnv =
    env ||
    new NunjucksLib.Environment(
      new NunjucksLib.FileSystemLoader([
        super.getIncludesDir(),
        TemplatePath.getWorkingDir()
      ])
fork icon0
star icon0
watch icon0

+ 2 other calls in file

56
57
58
59
60
61
62
63
64
65
66
    }).code}
    </script>`);
  };
}


let env = new nunjucks.Environment(new nunjucks.FileSystemLoader());
env.addExtension('StyleExtension', new StyleExtension());
env.addExtension('ScriptExtension', new ScriptExtension());
env.addFilter('raw', str => {
  return env.renderString(str, this.ctx);
fork icon0
star icon0
watch icon0

+ 2 other calls in file

178
179
180
181
182
183
184
185
186
187
188
  return e;
};


class Tag {
  constructor() {
    this.env = new Environment(null, {
      autoescape: false
    });
  }

fork icon0
star icon0
watch icon0