7

I'd like the rendered markup to be absent any carriage returns, line feeds, or tabs. I'm looking for a functionality similar to Smarty's {strip} tag. Does EE have this?

5 Answers 5

7

not natively but something like http://devot-ee.com/add-ons/hacksaw could easily do what you need. There's also http://devot-ee.com/add-ons/supergeekery-tag-stripper as well as some commercial add-ons that also give this kind of functionality.

6
  • It's actually a simple regex to do this. I'm new to EE. What do I need to develop that will let me "wrap" the template files with a pair of tags (presumably?) in order to remove CR, LF, and tabs and replace with an empty string? Nov 26, 2012 at 1:08
  • You would either need to enable PHP within a template and then do it that way...or...the recommended method would be to build a plugin with your PHP and the tags could be whatever you wanted. Nov 26, 2012 at 2:02
  • If I'm not mistaken there are already plugins out there that let you supply the regex search and replace as well.
    – UltraBob
    Nov 26, 2012 at 2:07
  • 1
    yeah Bob, there are gotolow.com/addons/low-replace is an example (an excellent example at that). If he wanted his own then that's how he'd do it. Nov 26, 2012 at 2:11
  • 1
    I've used ce_string's swap_breaks for this before. Nov 27, 2012 at 4:49
8

How about John Wells' Minimee? http://johndwells.github.com/Minimee/

It minifies the rendered HTML (EE 2.4+), and as far as I know, that's what you're looking for? I've only used it for CSS/JS and it works fabulously for that.

3
  • 1
    I would also go for this. Although I have noticed that it has a somewhat irritating habit of adding random new lines within the minified HTML.
    – Mutual
    Nov 26, 2012 at 23:17
  • 1
    Second for Minimee - does a great job of combining, minifying and caching js and css, but also can be set to strip and compress your HTML. For what you are looking for it doesn't require touching your templates either- just install and turn it on and you'll get lighter HTML. Nov 27, 2012 at 20:27
  • 1
    Also recommend Minimee. We use it on all our sites now for page speed optimisation reasons. Nov 27, 2012 at 20:39
2

A plugin is not really appropriate for this usage since you would need to wrap every root-level template you have with said plugin. Instead, you should write an extension which does the following:

  1. Check if the PHP constant REQ == 'PAGE', if not, do nothing
  2. Register a shutdown function
  3. Turn on output buffering
  4. In the shutdown function, capture the buffer
  5. Turn off output buffering
  6. Run your regex on the resulting page text and print it

I have used this technique to accomplish several similar requirements.

2
  • Wrap every template with said plugin? I think that's what I want. I would want to be able to control what to strip and what not to strip. Nov 26, 2012 at 22:32
  • I think you should go all the way and do this on every page if you're going to bother doing it at all. Otherwise what is the point? Of course the code should make sure not to compress things that shouldn't be compressed, but you're better off thinking about how to do that globally rather than putting a redundant tag all over the place. You will also run into tag caching issues if the tag does not take unique parameter for each place where it is used. This cannot be turned off - if you go that route just make sure to put a unique parameter on the tag even if it isn't used. Nov 26, 2012 at 22:55
1

Remember, you need to be careful stripping all whitespace from HTML, especially HTML generated by a CMS/where you can't be certain of the origin of every line of markup (eg some of it comes from WYSIWYG fields etc).

Although commonly thought of whitespace agnostic, HTML is a fragile language and some of that whitespace might well actually be significant, for example content in <pre> or <code> elements. There's also CSS like as white-space: pre to bear in mind. This sort of thing can be a nightmare to debug.

If you're doing this for the purposes of reducing the size of the pageload sent to the client, I'd argue you are far better off enabling gzip (with mod_deflate or similar) to this end, as with gzip enabled the benefits to payload size of stripping whitespace are often not worth the trade off in increased fragility/unpredictability of output.

3
  • Thanks for the warning about <pre> and <code>. I'm already compressing the output. I want to also remove unnecessary white space, though. Nov 26, 2012 at 22:33
  • Like I say, this is hard to do safely without parsing the HTML in some way, and that way madness lies... Any particular reason you want to do this? Have you benchmarked the performance gains (if you're doing this for that reason)?
    – Tom Davies
    Nov 26, 2012 at 22:36
  • It's a micro optimization effort. Nov 28, 2012 at 20:27
0

Have you considered/tried PHPStringFun (documentation)? With it you ought to be able to use PHP's native trim.

Something like the below perhaps?

{exp:phpstringfun function="trim" par1="\r"}
{exp:phpstringfun function="trim" par1="\n"}{html}{/exp:phpstringfun}
{/exp:phpstringfun}

Maybe just one \r\n would do, I dunno.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.