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, i can’t find any simple example of it)

Problem courtesy of: kulebyashik


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.

Solution courtesy of: Raynos

View additional discussion.