[2.x scala] <!DOCTYPE html> whitespace before

926 views
Skip to first unread message

Leon Radley

unread,
May 10, 2012, 9:49:06 AM5/10/12
to play-fr...@googlegroups.com
I'm having problems with IE telling me

Document mode restart from Quirks to IE9 Standards

It's because the doctype isn't at the very top of the html file.

I've tried huddling everything up to not have any spaces, bit even then I get two new lines at the top.

Is there any way to force the <!DOCTYPE html> to be at the very top?

Kevin Bosman

unread,
May 11, 2012, 7:38:01 AM5/11/12
to play-fr...@googlegroups.com
> Is there any way to force the <!DOCTYPE html> to be at the very top?

Leon, I've had this same problem in the Java api, which I've solved by
simply calling trim() on the Html body before returning it as a
Result, like this:
public static Content trim(Content content) {
if (content instanceof Html)
return new Html(content.body().trim());
else
return content;
}


Now I was wondering, do you think it would be generally reasonable and
acceptable to enforce/ensure that there is *never* any whitespace at
the top of *any* Html response? Or do you think that is assuming too
much? (I'm not sure what the W3C spec says, and haven't had the time
to check up on it.)

Kevin Bosman

unread,
May 11, 2012, 7:56:22 AM5/11/12
to play-fr...@googlegroups.com
> I'm not sure what the W3C spec says

Ok according to the HTML spec (all versions), whitespace and comments
are allowed anywhere.

But does anybody ever actually *need* to put whitespace at the top of
an html document? Personally, I think that's ridiculous! :)

Leon Radley

unread,
May 11, 2012, 9:50:20 AM5/11/12
to play-fr...@googlegroups.com
After fishing around a bit in the ScalaTemplate code and looking at the generated scala template files I've come to the conclusion that there is a \n leak somewhere causing the newlines.

I found a couple of template files in /target/scala-2.9.1/src_managed/main/views/html/*
Even if I huddle the DOCTYPE html right next to the @() i still get new lines.


But I'm unsure where to look.

Any suggestions?

Kevin Bosman

unread,
May 11, 2012, 9:55:47 AM5/11/12
to play-fr...@googlegroups.com
> \n leak somewhere causing the newlines.

That part, I presume, is unfortunately by design. It's not a leak per
se, but rather a faithful reproduction of *everything* between valid
scala templating code snippets, and that includes whitespace at the
end of your parameter line + at the end of every nested template.

If you think about it, you can't really assume that anything between
@code blocks was not intended, and that includes whitespace.

Unfortunately, this leads to rather unsatisfying output.

Kevin Bosman

unread,
May 11, 2012, 10:03:44 AM5/11/12
to play-fr...@googlegroups.com
> Even if I huddle the DOCTYPE html right next to the @() i still get new
> lines.

Have you tried squishing the <!doctype html> onto the end of the very
same 1st line that your parameters are on?

eg.
@(title: String, content: Html)<!doctype html>

That seems to work for me.

Leon

unread,
May 11, 2012, 10:23:57 AM5/11/12
to play-fr...@googlegroups.com
It shure did.. thought I tried this and didn't get it to work.. :P

The "Problem" if you should call it that, is that since you call the @main from another template you usually don't put the declaration of that template and the call to @main on the same line. but by doing so you can get rid of the whitespace. as follows.

@(message: String)@main("Welcome to Play 2.0") {

Could it be possible to supply a master template through implicits. That would be a cool :) and by doing so the whitespace would be avoided.

Something like wrapping a Action in a Authenticated method.

Just a thought.


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.


Carlos

unread,
Jul 19, 2012, 2:40:00 AM7/19/12
to play-fr...@googlegroups.com
This is by far the single most annoying aspect I've found in my first week of using Play, so I'm pretty happy.  
The extra newlines are tolerable, but losing indentation is really a bummer.  

John D

unread,
Jan 17, 2013, 5:17:38 PM1/17/13
to play-fr...@googlegroups.com
This may not really be an issue with HTML, but I'm working on some XML templates for integration with a 3rd party service, and the xml is considered invalid because there is one line of whitespace before my <?xml version="1.0" encoding="UTF-8"?>

This page contains the following errors:
error on line 2 at column 6: XML declaration allowed only at the start of the document

you still get the xml back (e.g. if you use curl), but very annoying for development...



On Friday, January 4, 2013 5:00:24 AM UTC-5, Romain Piel wrote:
I totally agree, there should be a way to clean up the rendered Scala templates. I've asked a question on Stack Overflow if you guys found a workaround.

Daniel Manchester

unread,
Jan 17, 2013, 10:11:11 PM1/17/13
to play-fr...@googlegroups.com
Hi,

For what it's worth, an XML template in my application doesn't seem to exhibit that problem; the first character of its output is the first character of the XML declaration, as one would want.

The only things that seem notable about the template are that the XML declaration is on the first line, immediately after the argument list; and that it's named "....scala.xml", as opposed to "....scala.html".

Dan

Volker Hatz

unread,
Jan 18, 2013, 3:38:58 AM1/18/13
to play-fr...@googlegroups.com
I am rendering text templates and also get the leading newline character in there. Which is annoying since I am producing special bash scripts which may not have an empty line at the very beginning. So I have to strip the newline  template.substring(1) ...

Guillaume Bort

unread,
Jan 18, 2013, 4:36:35 AM1/18/13
to play-fr...@googlegroups.com
Have you tried:

@(.....)<!DOCTYPE html>

?


--
 
 



--
Guillaume Bort, http://guillaume.bort.fr

Vladimir Nicolici

unread,
Oct 29, 2015, 12:00:28 PM10/29/15
to play-framework
In my case, I'm generating a JavaScript expression that is not allowed to have new lines at the beginning.

And the JavaScript expression starts with an open brace. And of course, you can't put open braces on the first line.

You can do:


@(chart: models.Chart)bla


You can't do:


@(chart: models.Chart){



The only thing that worked was defining constants for { and } in a scala object:

val openingBrace = "{"

val closingBrace = "}"

Then using those in the template where I would usually not be allowed like this:

@(chart: models.Chart)@controllers.shared.Common.openingBrace


[snip]

@controllers.shared.Common.closingBrace



Annoying as hell. In the J2EE world JSPs have similar issues. But they have a configuration parameter that fixes that: <trim-directive-whitespaces>true</trim-directive-whitespaces>.

I think Play needs a similar parameter. I lost a few hours fighting this.
Reply all
Reply to author
Forward
0 new messages