- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在我的 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 快速方式是什么?
最佳答案
在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);
关于node.js - 如何将 Nunjucks addfilter 函数放在 app.js 以外的 Express.js 文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48331121/
我正在将 jetty 7 迁移到 jetty 8, 我收到此错误: no suitable method found for addFilter(Class,String,int) servletCo
我想知道 Display 类中的 addFilter 和 addListener 方法在概念上有什么区别? 最佳答案 我假设您指的是org.eclipse.swt.widgets.Display,如果
我正在创建一个基于 REST 的 Web 应用程序,以 AngularJS 作为前端,以 REST 为基础的后端(使用 Spring 4 )。我遵循此处找到的基于代码的配置方法:WebApplicat
在使用 Silex 时将自定义过滤器连接到 Twig 的正确方法是什么,但保持现有 twig.options 不变? 这就是我的意思。我有以下代码: $app->register(new Silex\
我需要一些帮助来清理 Zend_Form 并向元素添加过滤器。现在我的印象是,当您向表单添加过滤器时,发布表单时,过滤器将作为 Controller 中调度的一部分执行。 然而,当我测试我的表单时,令
在尝试将 spring 应用程序转换为 springboot 时,我在尝试启动应用程序时遇到以下错误: java.util.concurrent.ExecutionException: org.apa
我想要一些复合范围的键盘快捷键。有问题的复合 Material 位于选项卡文件夹中。我有一个小函数,它遍历我的组合的所有子项并为每个子项添加一个 KeyboardAdapter。 我遇到的问题是,当我
带有/login 的 URL 模式应该经过 LoginFilter,其中验证用户 ID 和密码 - 工作正常 带有/users/register 的 URL 模式不应通过任何文件管理器,但它始终通过
我的任务是防止addFilter()使用新的 Gutenberg API 而不是任何 WP PHP 在某些自定义帖子类型上运行。它目前已输入 editor.PostFeaturedImage钩子(Ho
通常我会添加 org.springframework.web.filter.DelegatingFilterProxy将这样的片段添加到 web.xml 中: springSecurityF
我已经在 tomcat 7.0 中部署了 web 项目(maven)。它曾经部署运行良好。但不知何故它不断给出错误 java.lang.NoSuchMethodException: org.apach
我正在我的 express app.js 文件中初始化 nunjucks,并在其中注册一个自定义的 addfilter 函数同一个文件就好了: // get needed packages con
我是一名优秀的程序员,十分优秀!