Prevent empty lines in ejs for loops

本小妞迷上赌 提交于 2019-12-21 17:40:58

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!