Jump to content


Photo

Remove all line breaks from file shown to visitor


  • Please log in to reply
9 replies to this topic

#1 Lars282

Lars282

    Newbie

  • Members
  • PipPipPip
  • 76 posts
  • 3

  • LocationLondon, UK; Frankfurt, Germany

Posted 14 May 2012 - 09:40 AM

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

#2 DaveP

DaveP

    Sr. Member

  • Members
  • PipPipPipPip
  • 285 posts
  • 135

  • LocationChorley, UK

Posted 14 May 2012 - 10:24 AM

<?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.
Twitter : Facebook : GitHub : G+ : Blog : Powered by C8H10N4O2 and C10H14N2

#3 Lars282

Lars282

    Newbie

  • Members
  • PipPipPip
  • 76 posts
  • 3

  • LocationLondon, UK; Frankfurt, Germany

Posted 14 May 2012 - 10:40 AM

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?

#4 Soma

Soma

    Hero Member

  • Moderators
  • 3,186 posts
  • 1743

  • LocationSH, Switzerland

Posted 14 May 2012 - 11:35 AM

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.

@somartist | modules created | support me, flattr my work flattr.com


#5 diogo

diogo

    Hero Member

  • Moderators
  • 1,984 posts
  • 1061

  • LocationPorto, Portugal

Posted 14 May 2012 - 12:07 PM

You can also fight the spaces with other frontend techniques. I think this article might help you http://css-tricks.co...block-elements/

#6 ryan

ryan

    Hero Member

  • Administrators
  • 5,771 posts
  • 3108

  • LocationAtlanta, GA

Posted 14 May 2012 - 12:37 PM

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.

#7 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,521 posts
  • 845

  • LocationVihti, Finland

Posted 14 May 2012 - 01:30 PM

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

#8 Soma

Soma

    Hero Member

  • Moderators
  • 3,186 posts
  • 1743

  • LocationSH, Switzerland

Posted 14 May 2012 - 01:42 PM

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.

@somartist | modules created | support me, flattr my work flattr.com


#9 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,521 posts
  • 845

  • LocationVihti, Finland

Posted 14 May 2012 - 01:45 PM

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

#10 Lars282

Lars282

    Newbie

  • Members
  • PipPipPip
  • 76 posts
  • 3

  • LocationLondon, UK; Frankfurt, Germany

Posted 17 May 2012 - 12:30 PM

Thanks guys!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users