Consider indentation and remove newlines generated for "empty" lines
See original GitHub issueGiven 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:
- Created 6 years ago
- Reactions:1
- Comments:16 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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")) }
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.