Jump to:

23367 Posts in 18143 Topics by 2863 members

General Questions

SilverStripe Forums » General Questions » Lots of empty lines inserted into html?

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

Page: 1 2
Go to End
Author Topic: 2515 Views
  • Samba Sam
    Avatar
    Community Member
    85 Posts

    Lots of empty lines inserted into html? Link to this post

    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

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: Lots of empty lines inserted into html? Link to this post

    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

  • gieser
    Avatar
    Community Member
    13 Posts

    Re: Lots of empty lines inserted into html? Link to this post

    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.

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: Lots of empty lines inserted into html? Link to this post

    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.

  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Re: Lots of empty lines inserted into html? Link to this post

    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.

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts
  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: Lots of empty lines inserted into html? Link to this post

    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).

  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Re: Lots of empty lines inserted into html? Link to this post

    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);

    2515 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.