Skip to content

nunjucks deprecated render function #313

@gillesgw

Description

@gillesgw

Hi,

I'm using consolidate with express and nunjucks : I need for some pretty simple cases to configure my own instance of nunjucks, for adding custom filters among other things.

I can see a way in the doc, but it doesn't work, I have a "engine.configure is not an option" error, as describe in #244 , marked as resolved.

I digged a little "exports.nunjuncks.render" in lib/consolidate.js, and maybe I'm missing something, but if I comment quite almost the function, it seems to works perfectly :

in my app :

const cons = require('consolidate');
const nunjucks = require('nunjucks');
cons.requires.nunjucks = nunjucks.configure();
// cons.requires.nunjucks.addFilter('foo') ...
app.engine('njk', cons.nunjucks);

and in consolidate : /lib/consolidate.js :

`exports.nunjucks.render = function(str, options, cb) {
return promisify(cb, function(cb) {

try {

  var engine = options.nunjucksEnv || requires.nunjucks || (requires.nunjucks = require('nunjucks'));

  var env = engine;
  //
  // // deprecated fallback support for express
  // // <https://github.com/tj/consolidate.js/pull/152>
  // // <https://github.com/tj/consolidate.js/pull/224>
  // if (options.settings && options.settings.views) {
  //   env = engine.configure(options.settings.views);
  // } else if (options.nunjucks && options.nunjucks.configure) {
  //   env = engine.configure.apply(engine, options.nunjucks.configure);
  // }
  //
  // //
  // // because `renderString` does not initiate loaders
  // // we must manually create a loader for it based off
  // // either `options.settings.views` or `options.nunjucks` or `options.nunjucks.root`
  // //
  // // <https://github.com/mozilla/nunjucks/issues/730>
  // // <https://github.com/crocodilejs/node-email-templates/issues/182>
  // //
  //
  // // so instead we simply check if we passed a custom loader
  // // otherwise we create a simple file based loader
  // if (options.loader) {
  //   env = new engine.Environment(options.loader);
  // } else if (options.settings && options.settings.views) {
  //   env = new engine.Environment(
  //     new engine.FileSystemLoader(options.settings.views)
  //   );
  // } else if (options.nunjucks && options.nunjucks.loader) {
  //   if (typeof options.nunjucks.loader === 'string') {
  //     env = new engine.Environment(new engine.FileSystemLoader(options.nunjucks.loader));
  //   } else {
  //     env = new engine.Environment(
  //       new engine.FileSystemLoader(
  //         options.nunjucks.loader[0],
  //         options.nunjucks.loader[1]
  //       )
  //     );
  //   }
  // }

  env.renderString(str, options, cb);
} catch (err) {
  throw cb(err);
}

});
};`

Is there a reason to keep this code, and if so, maybe we need to adapt it ? I didn't digged enough and I'm new to consolidate and nunjucks, but I can try to investigate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions