Create variables and require packages in EJS

109 views Asked by At

Is it possible to run custom JavaScript in EJS templates?

I'm looking for the equivalent of the following Jade code:

- var Fs = require("fs")
- var foo = 42;
| #{foo}

This will create the Fs and foo variables and display 42.

Does EJS have this power?

From what I see, the following snippet does the same thing (without requiring the fs package):

<% var foo = 42; %>
<%= foo %>

Is this the right way or is there a better solution?

1

There are 1 answers

3
Julien Rodrigues On BEST ANSWER

Yes it is!

As you can see here http://www.embeddedjs.com/

In the example they are writing things from an array

<% for(var i=0; i<supplies.length; i++) {%>
    <li><%= link_to(supplies[i], 'supplies/'+supplies[i]) %></li>
<% } %>