top*_*kip 5 ejs node.js express
我正在使用带有ejs的express(node.js的web框架).现在我想创建自己的过滤器,如ejs github页面所述:
To add a filter simply add a method to the .filters object:
ejs.filters.last = function(obj) {
return obj[obj.length - 1];
};
Run Code Online (Sandbox Code Playgroud)
问题:我如何访问该ejs对象?我在app.js中尝试过(天真):
ejs.filters.myfilter = function (obj) {
....
}
Run Code Online (Sandbox Code Playgroud)
这给了我错误:
ReferenceError: ejs is not defined
Run Code Online (Sandbox Code Playgroud)
ale*_*lex 10
您需要在应用程序中要求ejs并在其上设置自定义过滤器,这对于Express应用程序是可见的.另请注意您在视图中如何使用ejs过滤器<%=: data_to_be_filtered | your_filter %>
.
示例应用:
app.js
var app, express = require('express'), ejs = require('ejs');
ejs.filters.my_custom_filter = function(ary) {
return ary[ary.length - 1];
};
app = express.createServer();
app.configure(function() {
app.set('view options', { layout: false });
app.set('view engine', 'ejs');
});
app.get('/', function(req, res) {
res.render('index', { data: [1, 2, 3, 4, 5] });
});
app.listen(8080);
console.log('Server started on port 8080');
Run Code Online (Sandbox Code Playgroud)
index.ejs(位于/ views)
<%=: data | my_custom_filter %>
Run Code Online (Sandbox Code Playgroud)
直接从github下载代码:http://github.com/alessioalex/ejs_filters
更多信息结帐:https://github.com/visionmedia/ejs
归档时间: |
|
查看次数: |
4684 次 |
最近记录: |