How to use the nunjucks.Environment function in nunjucks

To help you get started, we’ve selected a few nunjucks examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pangolinjs / core / lib / html / render-nunjucks.js View on Github external
return new Promise((resolve, reject) => {
    // Create Nunjucks environment
    let env = new nunjucks.Environment(
      new nunjucks.FileSystemLoader(path.join(context, 'src'))
    )

    // Prefix URL to make it relative
    env.addFilter('relative', (url) => {
      if (file.split(path.sep)[0] === 'components') {
        return `../${url}`
      } else {
        return url
      }
    })

    // Add custom section tag
    env.addExtension('SectionExtension', new SectionExtension())

    // Add environment variables to Nunjucks
github patrickarlt / acetate / lib / Renderer.js View on Github external
reset() {
    this._prerenderers = [];

    this.loader = new Loader({
      sourceDir: this.sourceDir,
      logger: this.logger,
      errorHandler: error => {
        this.emitter.emit("renderer:error", {
          error
        });
        this.logger.error(error);
      }
    });

    this.nunjucks = new nunjucks.Environment(this.loader, {
      // dev: true
    });

    this.hljs = hljs;

    this.markdown = new MarkdownIt({
      html: true,
      linkify: true,
      langPrefix: "",
      highlight: (code, lang) => {
        if (lang === "text" || lang === "plain") {
          return code;
        }

        return lang
          ? this.hljs.highlight(lang, code).value
github pangolinjs / core / lib / nunjucks / loader.js View on Github external
const componentPath = path.join(__dirname, '../../ui/templates/component.njk')
const componentTemplate = readFileSync(componentPath)

/* Set up component environment */

const njComponentContext = path.join(__dirname, '../../ui/templates')
const njComponentLoader = new nunjucks.FileSystemLoader(njComponentContext)
const njComponentEnv = new nunjucks.Environment(njComponentLoader, { autoescape: false })

njComponentEnv.addExtension('static', new StaticExtension())

/* Set up preview environment */

const njPreviewContext = path.join(store.state.cwd, 'src')
const njpreviewLoader = new nunjucks.FileSystemLoader(njPreviewContext, { noCache: true })
const njPreviewEnv = new nunjucks.Environment(njpreviewLoader, { autoescape: false })

njPreviewEnv.addExtension('static', new StaticExtension())

// Add custom Nunjucks filters to preview environment
for (let filter in store.state.config.nunjucks.filters) {
  njPreviewEnv.addFilter(filter, store.state.config.nunjucks.filters[filter])
}

// Add custom Nunjucks tags to preview environment
for (let extension in store.state.config.nunjucks.extensions) {
  njPreviewEnv.addExtension(extension, new store.state.config.nunjucks.extensions[extension]())
}

/* Loader logic */

module.exports = function (src) {
github themouette / express-users / index.js View on Github external
});
    }


    // Template engine configuration
    // -----------------------------
    //
    // Use Nunjuck internally.
    // We don't rely on Express render system as we want the module to be
    // reusable.
    // It is still possible to override templates and even engine through
    // configuration.
    var nunjucksLoader = _.map(options.views, function (directory) {
        return new nunjucks.FileSystemLoader(directory);
    });
    var nunjucksEnv = new nunjucks.Environment(nunjucksLoader, {
                                    autoescape: true,
                                    watch: !options.cache
                                });
    // A reusable function to render a Nunjuck template/partial.
    // This should be recursive.
    function partial(name, data) {
        if (typeof(data.partial) === 'undefined') {
            data.partial = partial;
        }

        var tmpl = nunjucksEnv.getTemplate(name);
        return tmpl.render(data);
    }
    // Render a template.
    function render(res, template, data) {
        // make global values available
github patrickarlt / acetate / lib / components / nunjucks.js View on Github external
var fullpath = path.join(acetate.root, acetate.src, matches[0]);

        acetate.log.debug('nunjucks', 'loading %s', fullpath);

        return {
          src: fs.readFileSync(fullpath, 'utf-8').split(/^([\s\S]+)^-{3}$/m)[0],
          path: fullpath
        };
      } else {
        acetate.log.warn('nunjucks', 'could not find a template named %s', name);
        return null;
      }
    }
  });

  return new nunjucks.Environment(new Loader(acetate));
};
github mozilla / togetherjs / Gruntfile.js View on Github external
function translateFile(source, translation) {
    var env = new nunjucks.Environment(new nunjucks.FileSystemLoader("./"));
    var tmpl = env.getTemplate(source);
    return tmpl.render({
      gettext: function (string) {
        return translation[string] || string;
      }
    });
  }
github Pomax / flickrmirror / lib / nunjucksloader.js View on Github external
module.exports = function(app) {
  var nunjucks = require("nunjucks"),
      fsl = new nunjucks.FileSystemLoader('views'),
      env = new nunjucks.Environment(fsl);

  env.addFilter('nlbr', function(str) {
    return str.replace(/\n/g,"<br>");
  });

  env.express(app);


  return env;
};
github hanamura / gulp-minisite / src / engines / nunjucks.js View on Github external
module.exports = function(options) {
  options || (options = {});
  options.path || (options.path = 'template');
  options.markdown || (options.markdown = function(str) {
    if (!str) return str;
    return new nunjucks.runtime.SafeString(marked(str));
  });

  var env = new nunjucks.Environment(
    new nunjucks.FileSystemLoader(options.path),
    {noCache: true}
  );
  env.addFilter('markdown', options.markdown);

  return function(tmplName, tmplData) {
    return env.render(tmplName, tmplData);
  };
};
github reptar / reptar / lib / renderer / template.js View on Github external
export function configureTemplateEngine({ config, paths, noCache = false }) {
  const fileSystemLoader = new nunjucks.FileSystemLoader(paths, {
    noCache,
  });

  const env = new nunjucks.Environment(fileSystemLoader, {
    autoescape: false,
  });

  addBuiltinFilters(env, config);

  return env;
}
github nobitajs / nobita / nunjucks.js View on Github external
const Nunjucks = require('nunjucks');
const config = require('./config/config.default');
const nunjucks = new Nunjucks.Environment(new Nunjucks.FileSystemLoader(config.temp.path));
module.exports = nunjucks;