Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Lots of empty lines inserted into html?


Go to End
Reply


13 Posts   3112 Views

Avatar
Samba Sam

Community Member, 85 Posts

4 November 2009 at 7:38am

Edited: 04/11/2009 7:42am

Hi,
Does anyone know how to eliminate all the unnecessary empty lines inserted into the generated html code.

For example, this is taking from my main navigation. This is typical through out the file.

<ul id="Nav">
               
                  <li>
                                 
                        <a href="/" class="link">Home</a>
                     
                  </li>

               
                  <li>
                                 
                        <a href="/history/" class="section">History</a>
                     
                  </li>

As another example, there are seven empty lines between my h2 header and the following content div.

Thanks,
Sam

Avatar
dalesaurus

Community Member, 283 Posts

5 November 2009 at 6:54pm

This has to do with templates and any spaces with your markers. You can take a look at the compiled templates in your temp space to get an idea why.

You can pick between readable templates and spacey code, or concise code and smushed templates. Sorry but there isn't an inbetween at this time.

<p>
<% control Asdf %>
$Item.Nice
<% end_control %>
</p>
--------------------becomes -----------------
<p>

my item NICE

</p>

or

<p>
<% control Asdf %>{$Item.Nice}<% end_control %>
</p>
--------------------becomes -----------------
<p>
my item NICE
</p>

Personally I don't care what it looks like if it comes out, pareses correctly, and works in FF3/IE7/Safari 4 :)

Avatar
gieser

Community Member, 13 Posts

15 June 2010 at 12:01am

Edited: 15/06/2010 12:11am

I've found a quick and dirty way to remove those empty lines. But my solution involves modifying classes from the Silverstripe core, in fact you need to modify the Controller class. Also this change was only tested on Silverstripe 2.3.3 and as of now i don't know if this hack also works in newer versions (though I guess it will work for all 2.3 versions). Also it would be much better if this change could be done to the parsed templates, as it would not require to run the replacement at every request to your website. unfortunately you can not really use the same method in SSViewer where the templates are parsed.

You need to change the file /sapphire/core/control/Controller.php
Add the following line to the variable declarations (e.g. at the top so you find easily find it if this change breaks something):

public static $remove_empty_lines = false;


Now search for the following line in the function "handleRequest":

$this->response->setBody($body);


Before this line add the following:

if(self::$remove_empty_lines) $body = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $body);

If you want to enable this feature add the following line to your _config.php:

Controller::$remove_empty_lines = true;

I assure you to test you're code against this change, as I haven't done a lot of testing.

Avatar
dalesaurus

Community Member, 283 Posts

15 June 2010 at 1:44am

That is a pretty clean fix, geiser. From what I gather these newlines are a product of SSViewer->process() that creates the PHP file that gets compiled from the template. You can see in there where there is doc block syntax used, requiring a newline separation:

<<<SSVIEWER

content

SSVIEWER;

This is where all the newlines come in. I supposed this could be tightened up at the cost of readability of the compiled templates...not that I imagine many people spend time trying to debug the compiled version. Maybe enable pretty newline in Dev compilation only.

So your proposal may be a few milliseconds slower, but it is easier than the "proper" fix that involves some intense core changes.

Avatar
DeklinKelly

Community Member, 197 Posts

10 February 2011 at 6:10am

I modified gieser's solution to remove ALL excessive whitespace.

$body = preg_replace("/(?=\s\s)\s*?(\n)\s*|(\s)\s+/", "$1$2", trim($body));   

The code above removes all redundant whitespace, not just newlines.

This significantly reduces the file size of the rendered HTML. On my site, it reduced the size of rendered HTML by 40%. When used in combination with static caching, it significantly decreases loading time, especially on mobile devices.

The only drawback is that I hate modifying core sapphire code. What needs to be done to incorporate this into future releases of SilverStripe? Because it can be controlled with a config setting, I see no reason why this should not be included as a core feature.

Avatar
swaiba

Forum Moderator, 1805 Posts

10 February 2011 at 11:06pm

How about decorating the controller?
http://www.silverstripe.org/general-questions/show/11849#post295247

Avatar
swaiba

Forum Moderator, 1805 Posts

15 February 2011 at 11:05pm

While I understand that actually white space is parsed and clutters up the tree in browser - has anyone tried mod_deflate?
I just turned it on for our new server and the pages seem to be served much faster (sorry no metrics).

Avatar
DeklinKelly

Community Member, 197 Posts

16 February 2011 at 12:31am

mod_deflate is useful, but it is only available to people who have root access to an Apache server. People who use IIS are out of luck. Most people who use shared hosting accounts cannot use mod_deflate.

mod_deflate does not remove excessive whitespace. It employs gzip compression for file transfers. Both the server and the browser must support gzip compression, otherwise Apache sends the files uncompressed. Currently, most mobile phones do not support gzip compression so they do not benefit from mod_deflate.

My opinion is that mod_deflate should be used when possible however excessive whitespace should also be removed.

swaiba, have you tested this regex in a decorated controller?

preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $body);

Go to Top