I'm new to node.js. I'm trying to use EJS templates, and i've got a little obstacle. if i'm getting it right, all code inside <% %> is executed serverside. What i don't know is how can i use javascript functions,that are located in modules (js files) inside those tags. Should i include them smth like <script src="...">? but that means it will be sent to the browser but i don't want that (or do I?). What is the right architecture?
Second question is: What ejs helper is specifically? how can i make one? (srry, i can't find any simple example of it)
发布评论
评论(2)
听起来您在模板中使用了太多逻辑。您不想调用自定义函数,也不想使用那些 HTML 帮助程序。
相反,您想要做的是正确定义绑定到模板的数据模型,并将业务逻辑与数据模型包装在一起。
然后创建数据对象的实例并将其传递给 EJS。
你所走向的是tagoup地狱。模板的目标只是使用简单的循环和部分视图将 JSON 转换为 HTML。
It sounds like your using too much logic inside your template. You do not want to call custom functions, you do not want to use those HTML helpers.
What you want to do instead is define the data model bound to your template properly and wrap your business logic together with your data model.
Then create an instance of you data object and pass it to EJS.
What your leading towards is tagsoup hell. The templates objective is merely to turn JSON into HTML using simple loops and partial views.
老问题,但以防万一其他人在这里绊倒......
在 Express 3 中,您可以向 app.locals 添加帮助程序。例如:
这些可以在您的视图中访问,如下所示:
Old question, but in case anybody else stumbles over here...
In Express 3, you can add helpers to app.locals. Ex:
These would be accessible inside your views like this: