Jump to content

Remove all line breaks from file shown to visitor


Lars282
 Share

Recommended Posts

Hey!

Not sure the tile explains well what I am trying to do:

For as long as I can remember, it seems to make a difference (when you care about every single pixel) whether you write

<span>XX</span><span>XX</span><span>XX</span>

or

<span>XX</span>
<span>XX</span>
<span>XX</span>

In this example there is an extra pixel of space between the spans.

Now that most content is generated both manually and dynamically with PHP, this is an even bigger problem as there is no consistency.

How can I get rid of this problem?

I was thinking of somehow removing all tabs and line breaks from the delivered html file before displaying it to the user ... is that the best way and if so, how would I do that?

Thanks a million and best wishes,

Lars

Link to comment
Share on other sites

<?php
$text = trim($text);
$text = str_replace('\r', '', $text);
$text = str_replace('\n', '', $text);
?>

...in various combinations will remove whitespace characters from the ends and <return> & <newline> from anywhere in $text.

Link to comment
Share on other sites

Thanks!

The problem is that the output comes not only from php code. There are various pieces hard coded without php, and then using <?= ... ?> to add content - so I cannot use your suggestion here. But when looping through children, the output comes from the php code, where I could ... but because it is php code with no line breaks manually added, it already is 'minified'.

The question is how can I remove all the unneeded spaces/lines breaks/tabs from the file before displaying it to the visitor?

What I am thinking: the pages are loaded through some processwire bootstrap index.php/module ... can that be modified to achieve what I want?

Link to comment
Share on other sites

No, but you could create a module to $this->hookAfter("Page::render", $this, 'myrender'); into the Page::render and do it there.

Edit: or use a ob_start() php method to buffer the output and do a callback that does the replacement.

Link to comment
Share on other sites

Now that most content is generated both manually and dynamically with PHP, this is an even bigger problem as there is no consistency.

I'm not sure that I understand-- You should be in full control of your markup when you use ProcessWire, so whether you output with line breaks or not, or whatever you want your output style to be, is up to you. The only thing I can think of is perhaps TinyMCE? If so, I suppose that you could remove all whitespace between tags with something like this:

$page->body = preg_replace('/>[\s\r\n]+</s', '><', $page->body); 

...but I'd worry about unintended side effects, like situations where you really do want two tags on two lines. There is a purpose for whitespace in markup and it's probably not good to throw it out. But if you need to cover a specific situation, a regexp may be a good way to go.

Link to comment
Share on other sites

This might be interesting read for: http://css-tricks.co...block-elements/

Aka two posts above. ;)

I also wonder what exactly the case is that would make it need something like this. I know about the issue, but never really had to fight with it. Can you provide examples (test page?), I'm sure it can be avoided somehow without php string replacments.

Link to comment
Share on other sites

Aka two posts above. ;)

Hehe, I read this from mobile and right when I got here I had to post that link - didn't notice Diogo's post there. Kertaus on opintojen äiti, as we say here :) (Repetitio mater studiorum as they use to say).

Link to comment
Share on other sites

  • 1 year later...

is there any server side solution to this ? Otherwise I should maybe try and write a module myself.

All the client side solutions a pretty awful. I started using inline-block instead of float – much better, except for the space issue.

j

Link to comment
Share on other sites

is there any server side solution to this ? Otherwise I should maybe try and write a module myself.

All the client side solutions a pretty awful. I started using inline-block instead of float – much better, except for the space issue.

j

The http://modules.processwire.com/modules/all-in-one-minify/ does output minification. Use it myself a lot. If it suits your work-flow there's a lot more goodies you can use it for.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...