[2.1.3 Scala] How to get rid of newlines in the template

953 views
Skip to first unread message

Joe Zulli

unread,
Aug 21, 2013, 8:20:21 PM8/21/13
to play-fr...@googlegroups.com
Hi everyone,

I'm using Play templates to render some plain text emails for me. Everything is working fine, except for one big problem; there are newlines all over the place that I can't get rid of. It appears that every dynamic statement (anything starting with a "@") results in an empty line. In addition, even closing braces are resulting in a new line. For example, this:

-------------------------
@for(p <- products) {
 
@p.getName() ($@p.getPrice())
 
@for(c <- coupons) {
   
@c.title
 
}
}
--------------------------
Is being rendered as this:

-------------------------

 
Product 1 ($1.00)

    $0
.50 Off Product 1


-------------------------

Is there any way to fix this? I don't understand how a closing brace should ever translate to a newline, but who knows? I know I can obviously write a regex and brute force fix it in my controller, but that deletes newlines that are supposed to be in there. I've been searching around the web, and for html, the recommendation seems to be to use Google's HTMLCompressor, but I don't see any plaintext solution. 

Thanks in advance for the help!
Joe


Andrew Gaydenko

unread,
Aug 21, 2013, 9:39:14 PM8/21/13
to play-fr...@googlegroups.com
Both braces and @s are on the next line - renderer just literally copies all of them. Just remove not needed new lines (as well as white spaces) from template and add trim() to result.

Joe Zulli

unread,
Aug 22, 2013, 3:35:49 PM8/22/13
to play-fr...@googlegroups.com
Hi Andrew, 

Thanks for the response, but I'm not sure I understand. Trim only removes extraneous whitespace from the ends. What's important is removing it from the middle. How, for example, would I remove the newline in between the ------- and the product name in my example above? Other templating languages that I have used have all had hacks if not solutions for whitespace. For example, in Velocity, I would solve the problem by adding a comment directly after (and on the same line as the for declaration. I'm hoping there's something similar I can do in Play.

Joe

Andrew Gaydenko

unread,
Aug 22, 2013, 3:53:28 PM8/22/13
to play-fr...@googlegroups.com
On Thursday, August 22, 2013 11:35:49 PM UTC+4, Joe Zulli wrote:
Trim only removes extraneous whitespace from the ends.

Yes, you are right, and trimming can be useful for (at least) visual separating (I mean new line) of parameters list only.

 
What's important is removing it from the middle. How, for example, would I remove the newline in between the ------- and the product name in my example above?

You can align all in a single line and than add needed new lines and spaces step by step as needed

-------------------------@for(p <- products){@p.getName()($@p.getPrice())@for(c <- coupons){@c.title}}--------------------------

@-statements are neutral with regard to white spaces and new lines, as I can guess. So you manager your formatting literally.

biesior

unread,
Aug 22, 2013, 4:46:13 PM8/22/13
to play-fr...@googlegroups.com
W dniu czwartek, 22 sierpnia 2013 02:20:21 UTC+2 użytkownik Joe Zulli napisał:
Hi everyone,

I'm using Play templates to render some plain text emails for me. Everything is working fine, except for one big problem; there are newlines all over the place that I can't get rid of. It appears that every dynamic statement (anything starting with a "@") results in an empty line. In addition, even closing braces are resulting in a new line. 

Andrew Gaydenko

unread,
Aug 22, 2013, 4:53:56 PM8/22/13
to play-fr...@googlegroups.com
On Friday, August 23, 2013 12:46:13 AM UTC+4, biesior wrote:

The problem under discussion isn't related to html-compression and even to html at all. The aim is to get needed concrete formatting for plain text (look at the first topic message).

Joe Zulli

unread,
Aug 22, 2013, 5:01:48 PM8/22/13
to play-fr...@googlegroups.com
Hi Andrew,

Thanks. I will follow that advice. The code will be quite ugly, but I know I can get it to work at least :)

Joe

biesior

unread,
Aug 22, 2013, 5:06:46 PM8/22/13
to play-fr...@googlegroups.com
Good point, it can be controlled (a little) by placing closing brackets in the same line ie:

-------------------------
@for(p <- products) {
 
@p.getName() ($@p.getPrice())
 
@for(c <- coupons) {@c.title}}
--------------------------

If it's still not possible to get right result I'd recommend formatting directly in the controller (yes I now that;s a violation of MVC pattern)

Andrew Gaydenko

unread,
Aug 22, 2013, 5:09:35 PM8/22/13
to play-fr...@googlegroups.com
On Friday, August 23, 2013 1:01:48 AM UTC+4, Joe Zulli wrote:
Thanks. I will follow that advice. The code will be quite ugly, but I know I can get it to work at least :)

As far as anybody potentially can want any output formatting, there are not any design constraints to templating, and the only way is just to repeat all original (outside @-statements) chars literally. Any "clever beautification" can conflict with use cases where this beatification must be avoided. So, I guess, literal repeating is the best way. I agree, there is some ugliness in template code when needed formatting is got :)
Reply all
Reply to author
Forward
0 new messages