Nodejs EJS帮助函数? [英] Nodejs EJS helper functions?
本文介绍了Nodejs EJS帮助函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
app.js
ejs.helpers.sayHi = function(name){
return'Hello'+ name;
});
index.ejs
<%= sayHi('Bob')%>
解决方案
是的,在Express 3中,您可以添加帮助 app.locals
。例如:
app.locals.somevar =hello world;
app.locals.someHelper = function(name){
return(hello+ name);
}
这些可以在您的视图中访问,如下所示:
<%somevar%>
<%someHelper('world')%>
注意:Express 2.5的帮助者不一样。
Is there a way to register helper functions to EJS templates, so that they can be called from any EJS template? So, it should work something like this.
app.js
ejs.helpers.sayHi = function(name) {
return 'Hello ' + name;
});
index.ejs
<%= sayHi('Bob') %>
解决方案
Yes, in Express 3 you can add helpers to app.locals
. Ex:
app.locals.somevar = "hello world";
app.locals.someHelper = function(name) {
return ("hello " + name);
}
These would be accessible inside your views like this:
<% somevar %>
<% someHelper('world') %>
Note: Express 2.5 did helpers differently.
这篇关于Nodejs EJS帮助函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文