-
-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider indentation and remove newlines generated for "empty" lines #141
Comments
We could possibly trim the empty lines (a pull request would be good if you have the time). But I don't think we should touch indentation:
In other words, too many things to consider and not much value added, IMO. |
Just strip out all useless spaces during play For example, i'm using
Why? HTML templates generates a tons of useless spaces from identation, and mobile-device users suffering from more traffic, more parsing. Using |
@marcospereira The indentation is easy to work around. Just keep track of the spaces and tabs of the if and remove the same amount of indentation on the branches. That will cover 99% of the cases. If this heuristic fails revert to the original implementation which does not touch the indentation. @helllamer You have a different concern, what you want to achieve is minification. There exist already many tools capable of minifying HTML, CSS, JS, etc. |
Yes and no. Full minification of twirl templates is not possible, because they are stops to compile. My solution with "zero identation" and it is related to $subj: If someone will start to magic around twirl output identation, it will be good to implement identation using "" (empty strings, tweakable via sbt).
Yes, but for play-framework really where only one working for-years partial solution, and several hand-written crunches over grunt/gulp/bash/etc. |
Does https://github.com/sbt/sbt-uglify solve your problem? |
@Lasering uglify is only for assets pipeline, not for identated twirl templates. I have no problem, only thing i've saying: if someone will implement custom ident in twirl-generated files, it will be good to also allow for "zero identation". |
well I think if somebody from the community will step up and do this, there is no reason to don't include it. But i don't think that this should have any priority. Edit: and btw. there is no way of streaming/chunk twirl templates. or to say it differently. you would still have the whole template in memory since render()/apply() will return the full template string. |
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. |
This issue also bothers me. My template just like
but generated html like this
I expect at least like this
It should be good if support some config to the template output, such as cleanup empty line. In general, it shouldn't generate empty line for the @import or other statements which just for logic purpose. Or add some symbol to indicate that a line is just a logic statement, should not generate empty line. |
I opened a pull request which addresses some of the newline complains. See #169 |
#169 is merged, that should make the newline behaviour a bit better. |
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: twirl/compiler/src/main/scala/play/twirl/compiler/TwirlCompiler.scala Lines 177 to 207 in fe20c77
And then navigate the code from there. |
This is more for curiosity: Why does the TwirlCompiler code uses The code in
Would be much easier to read in this form
Also I cannot understand why the generated templates include the position as a comment everywhere (eg: |
You are free to change that, but I would do that as a separated change to keep contributions on small and easier to review. :-)
Yeah, they are important. The positions are a "source map" that provides a way of mapping code generated by Twirl back to its original position in a Long story short, removing them will break things. |
Aren't the positions redundant information with https://github.com/playframework/twirl/blob/master/compiler/src/main/scala/play/twirl/compiler/TwirlCompiler.scala#L733? Especially with If that information is so important shouldn't it be reified is some data structure that would be generated along side with the generated class? |
Given this template:
The generated html will be:
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.The text was updated successfully, but these errors were encountered: