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
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.
-
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.– UltraBobNov 26, 2012 at 2:07
-
1yeah 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
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.
-
1I 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.– MutualNov 26, 2012 at 23:17
-
1Second 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
-
1Also recommend Minimee. We use it on all our sites now for page speed optimisation reasons. Nov 27, 2012 at 20:39
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:
- Check if the PHP constant REQ == 'PAGE', if not, do nothing
- Register a shutdown function
- Turn on output buffering
- In the shutdown function, capture the buffer
- Turn off output buffering
- Run your regex on the resulting page text and print it
I have used this technique to accomplish several similar requirements.
-
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
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.
-
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)? Nov 26, 2012 at 22:36
-
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.