我正在尝试在服务器端使用ejs创建Web应用程序的非JavaScript版本。我将一个包含应用程序状态的对象传递到模板中,有一次我想使用该状态对象构建一个url。所以基本上我想做类似<%= makeUrl(objectState.data [0])%>
如何在ejs模板中使makeUrl可调用? / p>
谢谢
编辑:我知道我可以将函数作为参数传递给模板,但是在那里
解决方案在Express 3中,他们删除了动态助手的概念。我相信实际上建议通过app.locals将函数传递到模板中。我知道您已经知道了,但是对于其他有相同问题的人:
在您的app.js中: app.locals.myFunc =函数(arg){...} 在您的模板中:<%= myFunc(objectState.data [0])%>
I am trying to create a non-javascript version of my web app using ejs on the server side. I pass into the template an object containing the app's state, and at one point I want to build a url using that state object. So basically I want to do something like <%=makeUrl(objectState.data[0])%>
how can I make makeUrl callable from within ejs templates?
Thanks
edit: I know I can pass a function in as a parameter to the template, but is there a better way?
解决方案in Express 3, they removed the concept of dynamic helpers. I believe that passing functions into the template via app.locals is in fact the recommended way to do this now. I gather you already know how, but for anybody else with this same question:
in your app.js: app.locals.myFunc = function(arg){...} in your template: <%= myFunc(objectState.data[0]) %>
更多推荐
从节点上的ejs模板调用函数
发布评论