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.

Consider indentation and remove newlines generated for "empty" lines

See original GitHub issue

Given this template:

@(list: List[String])

<html>
  <body>
    @if(list.isEmpty) {
      <h1>Nothing to display</h1>
    } else {
      <h1>@list.size items!</h1>
    }
  </body>
</html>

The generated html will be:



<html>
  <body>
    
      <h1>2 items!</h1>
    
  </body>
</html>

Notice that the h1 tag has an extra indentation level and it is surrounded by empty lines. This is happening because of the @if. Twirl could be smarter (and way more awesome) if it would remove a level of indentation from the branches of the if.

It could also remove the generated empty lines from the if. Please note that if the if was declared like this: @if(list.isEmpty) { <h1>Nothing to display</h1> } else { <h1>@list.size items!</h1> } no newlines are created.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
AlbaroPereyracommented, Sep 7, 2017

Indentation aside, in my humble opinion, I believe the lines created by twirl are annoying. Who really writes twirl templates without line breaks {code here}{some more code here}.

I created this patch for the Play Framework. It might be helpful when addressing this issue. def prettify(content: Html): Html = { Html(content.body.trim().replaceAll("\\n\\s*\\n", "\n")) }

1reaction
marcospereiracommented, Jul 30, 2019

I have the time to do a PR. I just need a little bit of guidance: where should I look at? The parser?

Hey @Lasering, thanks for taking the time to work on this.

You can start here:

https://github.com/playframework/twirl/blob/fe20c77f0bb7737efc7d38cbcf56c57786604698/compiler/src/main/scala/play/twirl/compiler/TwirlCompiler.scala#L177-L207

And then navigate the code from there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python indentation in "empty lines" - Stack Overflow
This answer doesn't add anything useful whatsoever. Since in Python indentation is mandatory, PEP8 should favor indented empty lines over "empty ...
Read more >
Get same indentation behavior as opening a new line on an ...
The solution is to use S on the empty line in normal mode. If you search for documentation on it via :help S...
Read more >
How to trim indent when adding extra line? – Rider Support
To automatically remove indents on empty lines and strip trailing spaces, you can use File | Settings | Editor | General -> Strip...
Read more >
Rationale - Prettier
It turns out that empty lines are very hard to automatically generate. The approach that Prettier ... …all you need to do is...
Read more >
White-space handling - Apache FreeMarker Manual
Indentation white-space, and trailing white-space at the end of the line (includes the line break) will be ignored in lines that contains only ......
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