question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Newline slurp and indentation

See original GitHub issue

Lately, I’ve been trying to use the trim-mode closing tag -%> and I understand what it’s doing but I wonder if the current behaviour does make sense or not really. So it’s more of an open question than an issue. I was thinking, but I might be wrong, that the purpose of this tag was to write such template:

<ul>
<% users.forEach(function (user) { -%>
  <li><%= user %></li>
<% }) -%>
</ul>

And it would output something like:

<ul>
  <li>John</li>
  <li>Jane</li>
</ul>

And actually, it will! Nice. But let’s take a code just a little bit more complex with more indentation.

<div>
  <ul>
  <% users.forEach(function (user) { -%>
    <li><%= user %></li>
  <% }) -%>
  </ul>
</div>

I would expect:

<div>
  <ul>
    <li>John</li>
    <li>Jane</li>
  </ul>
</div>

But I got:

<div>
  <ul>
        <li>John</li>
        <li>Jane</li>
      </ul>
</div>

That’s because -%> only remove the following new line (just as mentioned in the doc), meaning it will concatenate both the indentation of the EJS tag and the indentation of the following line. Not sure this makes sense. So the deeper your EJS tag, the worse it will become.

Don’t you think it would be better to remove the following new line and any following white spaces? This way, you could write:

<div>
  <ul>
    <% users.forEach(function (user) { -%>
    <li><%= user %></li>
    <% }) -%>
  </ul>
</div>

(yeah, it’s not exactly the same as the previous one because the EJS tag would now needs to be at the same indentation as its content). This would output:

<div>
  <ul>
    <li>John</li>
    <li>Jane</li>
  </ul>
</div>

Another solution would be to have a syntax to indicate that an opening tag should remove all white spaces before him, meaning that the tag is actually only indented to be more readable but we don’t actually want to output it’s indentation. I would have suggest to use <%- but since it’s already taken, I have no idea so I will use <%+ but that’s just for the purpose of the sample. Pro: you can indent any EJS tag as you want. Con: you have to use both a custom opening and closing tags to remove everything you don’t want.

<div>
  <ul>
  <%+ users.forEach(function (user) { -%>
    <li><%= user %></li>
  <%+ }) -%>
  </ul>
</div>

Would output:

<div>
  <ul>
    <li>John</li>
    <li>Jane</li>
  </ul>
</div>

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

18reactions
andidevcommented, Aug 8, 2015

The proposed PR #105 introduces the following new syntax

<%_ removes any spaces or tabs before the ejs-tag _%> removes any spaces or tabs and a newline after the ejs-tag

this means one can write

<div>
  <ul>
  <%_ users.forEach(function (user) { _%>
    <li><%= user %></li>
  <%_ }) _%>
  </ul>
</div>

and then get the requested output mentioned in this issue

<div>
  <ul>
    <li>John</li>
    <li>Jane</li>
  </ul>
</div>
0reactions
mdecommented, Sep 7, 2015

Apologies for the lengthy wait on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to change the indentation when entering a new line?
Hi guys, How to change the indentation when entering a new line? Sorry for the probability silly question.
Read more >
Permissions — Smartparens 1.10.1 documentation
slurp -forward - run in forward-slurp before/after the closing delimiter is moved; slurp-backward ... (newline) (indent-according-to-mode) (forward-line -1) ...
Read more >
7.7 #slurp - Cheetah
7.7 #slurp. Syntax: #slurp. The #slurp directive eats up the trailing newline on the line it appears in, joining the following line onto...
Read more >
Parinfer - simpler Lisp editing - Shaun Lebron
Parinfer is a proof-of-concept editor mode for Lisp programming languages. It simplifies the way we write Lisp by auto-adjusting parens when indentation ......
Read more >
Matching multiple line indents in Perl - Stack Overflow
You can still slurp your whole file into $line and then do (destructively): while ( $line =~ s/\n([\t ]+)// ) { my $count...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found