in Uncategorised

Different ways of rendering a variable using EJS

The simplest way of rendering a variable is using the scriptlets, similar to the ones used in JSPs

<%= variable %>

But if you want your code to be rendered or executed but not printed then render it like this:

<% variable %>

If you want your code to be evaluated as well as rendered (ex: variables containing HTML code) then use:

<%- variable %> <!-- See the use of minus symbol within the scriptlets -->

We usually use the last method to render HTML on our EJS pages.

Write a Comment