从节点上的ejs模板调用函数 [英] call functions from with ejs templates on node

查看:260
本文介绍了从节点上的ejs模板调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在服务器端使用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模板调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆