【问题标题】:How to put Nunjucks addfilter functions in Express.js file other than app.js?如何将 Nunjucks addfilter 函数放在除 app.js 之外的 Express.js 文件中?
【发布时间】:2018-06-28 02:32:05
【问题描述】:

我在我的express app.js 文件中初始化nunjucks,并在同一个文件中注册一个自定义addfilter 函数就好了:

  // get needed packages
const nunjucks = require('nunjucks');

  // config view engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');

  // set variable
const env = nunjucks.configure('views', {
  autoescape: true,
  express: app
});

  // register custom helper
env.addFilter('shorten', function(str, count) {
  return str.slice(0, count || 5);
});

但是,我想添加更多这些addfilter 函数,但我不想将它们放入我的app.js 文件中。具体来说,我想把它们放在这里:

node-project/views/helpers/nunjucks_helpers.js

配置此包以在所述其他文件中注册像这样的自定义过滤器的节点快速方式是什么?

【问题讨论】:

    标签: node.js express module require nunjucks


    【解决方案1】:

    最少的代码更改

    在nunjucks_helpers.js中创建一个以env为参数的函数并导出:

    // helpers/nunjucks_helpers.js
    function addNunjucksFilters(nunjucksEnvironment) {
      nunjucksEnvironment.addFilter(...);
      // Add all your other calls to addFilter here
    }
    
    module.exports = addNunjucksFilters;
    

    然后将其导入app.js并调用它:

    // app.js
    var addNunjucksFilters = require('./helpers/nunjucks_helpers.js'); // Path might be different - depends on where you put app.js
    // ... your existing code
    addNunjucksFilters(env);
    

    更多关于包含来自其他文件in this Q and A的函数的信息。

    关注点分离

    为了更好地分离关注点,您可以将所有与 nunjucks 相关的内容移出 app.js:

    // helpers/nunjucks-helper.js:
    const nunjucks = require('nunjucks');
    
    function setUpNunjucks(expressApp) {
      const env = nunjucks.configure('views', {
        autoescape: true,
        express: app
      });
    
      // register custom helper
      env.addFilter('shorten', function(str, count) {
        return str.slice(0, count || 5);
      });
      // ... your other filters here
    }
    

    这会让你的 app.js 看起来更干净:

    // app.js
    const setUpNunjucks = require('./helpers/nunjucks_helpers.js');
    
    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'html');
    setUpNunjucks(app);
    

    【讨论】:

    • 完美运行!我对这个功能感到惊讶,并最终看到在导出模块时需要文件对组织有多么强大。我已经查看了您之前留下的线程,但是如果没有这样一个明确的演示,我是否无法将其应用于我自己的问题。谢谢!
    • 谢谢,我非常感谢您的回复显示了解决问题的多种方法 - 它们让我们成为了更好的工程师。
    • 这值得更多的支持。在 Express 4 中效果很好
    【解决方案2】:

    我的原始代码中有一个错误,它进入了 Stone 答案的 Separation of Concerns 部分。这是一个很棒的提示,如果您更改该块以正确设置 nunjucks env 变量,一切都会正常工作:

    // helpers/nunjucks-helper.js:
    const nunjucks = require('nunjucks');
    
    function setUpNunjucks(expressApp) {
      // set variable
      const env = nunjucks.configure('views', {
        autoescape: true,
        express: app
      });
    
      // register custom helper
      env.addFilter('shorten', function(str, count) {
        return str.slice(0, count || 5);
      });
      // ... your other filters here
    }
    

    【讨论】:

    • 更新了我的答案。感谢您发布更正。
    猜你喜欢
    • 2022-01-08
    • 1970-01-01
    • 2013-02-04
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 2019-05-16
    相关资源
    最近更新 更多