开发者

node.js + EJS + using javascript inside <% tags

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开发者_如何学C, i can't find any simple example of it)


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.


Old question, but in case anybody else stumbles over here...

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') %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜