Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent empty lines in ejs for loops

Tags:

javascript

ejs

I have the following simple ejs template:

<% for (var i =0; i < 10; ++i) { %>
    - <%- i %>
<% } %>

This renders the following:

- 0

- 1

- 2

- 3

- 4

- 5

- 6

- 7

- 8

- 9

How can I prevent ejs to create empty lines like these?

If possible I don't want to modify the result string but to tell ejs to not render these empty lines. How can I do that?

like image 744
Ionică Bizău Avatar asked Dec 15 '22 07:12

Ionică Bizău


1 Answers

Trim-mode ('newline slurp') tag, trims following newline.

 <% i -%> or <% -%> or <%= i -%> or <%- i -%>

All you need is the hypen.

In the Features section, there you can see this option:

  • Newline-trim mode ('newline slurping') with -%> ending tag

Note that the new version of ejs is maintained at mde/ejs (not tj/ejs anymore).

like image 192
Kim Avatar answered Dec 27 '22 14:12

Kim