从节点上的ejs模板调用函数

编程入门 行业动态 更新时间:2024-10-27 20:32:11
本文介绍了从节点上的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模板调用函数

本文发布于:2023-10-29 12:54:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539743.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   函数   模板   ejs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!