问题
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?
回答1:
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).
回答2:
If you add a '-' before the "%>", it will make line break
<% for (var i =0; i < 10; ++i) { %>
- <%- i -%>
<% } %>
But I found it quite tricky to use, because it not always work as expected.
This issue has more details on it.
来源:https://stackoverflow.com/questions/36175454/prevent-empty-lines-in-ejs-for-loops