Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

$smarty ->display(tpl,cacheid,compress)
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
hinrichs
Smarty Rookie


Joined: 24 Apr 2003
Posts: 28
Location: San Francisco, CA

PostPosted: Fri Apr 25, 2003 4:02 pm    Post subject: $smarty ->display(tpl,cacheid,compress) Reply with quote

Before caching the html, or maybe before rendering a noncached page (not sure about overhead there), it would be very cool if smarty could run a simple ereg that would compress the html in the template for output. This can greatly cut down on the bandwidth needed to serve pages on a popular site, especially these days when site pages are so table-heavy. It is great when developers can make pages (templates) without having to worry about adding extra spaces, tabs, etc. to make it easier to maitain, in its effect on bandwidth.
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Apr 25, 2003 9:24 pm    Post subject: Reply with quote

That is not a function of Smarty. If you want to compress the output, use fetch() and then compress it in your application. The best solution IMHO is to use mod_gzip with Apache. This is 100% transparent to your applications, and this will cover static content as well as dynamic (PHP, Perl, etc.), it will handle quirkyness of buggy/old browsers, it will cache .gz files, it is extremely efficient since it is done at the apache layer, etc.

Monte


Last edited by mohrt on Fri Apr 25, 2003 9:38 pm; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website
toma
Smarty Regular


Joined: 25 Apr 2003
Posts: 62

PostPosted: Fri Apr 25, 2003 9:27 pm    Post subject: Tom Reply with quote

Using the php.ini or output buffering is the easiest way:

ob_start("ob_gzhandler");
http://www.php.net/manual/en/function.ob-gzhandler.php
Back to top
View user's profile Send private message Visit poster's website
hinrichs
Smarty Rookie


Joined: 24 Apr 2003
Posts: 28
Location: San Francisco, CA

PostPosted: Fri Apr 25, 2003 10:57 pm    Post subject: Reply with quote

The documentation on this page states "Also note that using ini.zlib.output_compression is preferred over ob_gzhandler()."

I have not yet looked into doing this on the apache level (which sounds attractive). But how do I test to see if it is working? When I add the ob_gzhandler line or set this transparent compression value in the php.ini, then access the page, everything looks exactly the same as before. If compression is working, this is probably good. However I can't find any suggestions on how to test to see if it is working and how to test the results. Also I am concerned about side-effects, such as whether the browser will need to spend time "decompressing", and compatibility among browser types.
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Apr 25, 2003 11:20 pm    Post subject: Reply with quote

yeah, it is compression over the wire, so the browser has to be able to accept gzip--which almost all do. I'm not sure what benefit you'd get out of compression without a decompression stage at the client. Otherwise it seems rather gratuitus (and costly) to compress a file for storage only to uncompress it prior to transmission.
Back to top
View user's profile Send private message
hinrichs
Smarty Rookie


Joined: 24 Apr 2003
Posts: 28
Location: San Francisco, CA

PostPosted: Sat Apr 26, 2003 12:05 am    Post subject: Reply with quote

Thanks for your reply.

It looks from the documentation that php 4.3 can allow one to change the ini zlib settings at the page level, and that using the zlib.output_compression is the recommended method.

But, again, does anyone out there know how I can examine the results of turning this on? I cannot even tell if it is working or not.
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sat Apr 26, 2003 12:11 am    Post subject: Reply with quote

i'd use "ngrep '' port 80" and open the page with a browser. if i can read the stuff that is dumped to the console, then zlib-compression was definately off. Wink

ngrep can be found at http://www.packetfactory.net/Projects/ngrep/ but i'm sure that there are "smarter" ways to analyze this. Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat Apr 26, 2003 12:36 am    Post subject: Reply with quote

This doesn't require ngrep.

Use telnet:

C:\>telnet localhost 80

Use localhost or whatever name (or address) of the host you want to test.

NOTE: you get a blank screen... your keypresses won't be echoed, so don't make any spelling errrs Wink

Now type the following, changing it to match the path to your page:

get /yoursite/yourfile.php

Hit enter and you should see scrambled toast if gzip is on. That's because telnet doesn't support gzip.
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sat Apr 26, 2003 12:45 am    Post subject: Reply with quote

hmm. first of all you should prefer nc over telnet wherever possible, of course Smile

second: http is simple, but not that simple. you have to pretend that you accept gzip-compressed encoding via the correct headers. otherwise it would only be transparent to the clients that know it at all. that wouldn't be backwards compatible.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat Apr 26, 2003 1:23 am    Post subject: Reply with quote

Nothing escapes your eagle eye, messju!

Sure telnet may be the bad girl in your toolbox, but sometimes, when onsite, she can be your only friend.

In my last example, once you are connected to your site, instead of typing the get, modify the following as appropriate, paste it into the pastebuffer, right-click in the command window (to paste) and hit enter.

Code:
get /yoursite/yourfile.php HTTP/1.1
Accept: */*
Accept-Language: en-us
Host: localhost
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
Accept-Encoding: gzip


The output will look something like:

Quote:
HTTP/1.1 200 OK
Date: Sat, 26 Apr 2003 01:21:08 GMT
Server: Apache/1.3.27 (Win32) PHP/4.3.0
X-Powered-By: PHP/4.3.0
Content-Encoding: gzip
Vary: Accept-Encoding
Transfer-Encoding: chunked
Content-Type: text/html

276
▼♥∞T█j█@►}_╚?êàB☻i$ ......


Notice the Content-Encoding in the returned header and that the data returned is assuredly non text.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat Apr 26, 2003 1:30 am    Post subject: Reply with quote

oh yeah, I was intending this mainly for windows (C:/>) which has a dearth of tools available Smile

edit:

I should note that PHP 4.3+ allows you to read the headers of an open stream, so you could write a cli script to do this, too. Hmm.

update: just tested PHP 4.3 and stream_get_meta_data() doesn't return all headers. Oh well.
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sat Apr 26, 2003 10:29 am    Post subject: Reply with quote

thats what i did some time ago for another purpose:
(i just added the accept-encoding-header)
Code:

<?php

$url='http://test.com/test';

$u = parse_url($url);

if (!isset($u['port'])) $u['port']=80;
if (!isset($u['path'])) $u['path'] = '/';
if (isset($u['query'])) $u['path'] .= '?' . $u['query'];

if ($s=fsockopen($u['host'],$u['port'])) {
   fputs($s, "GET ".$u['path']." HTTP/1.1\r\n");
   fputs($s, "Host: ".$u['host']."\r\n");
   fputs($s, "Accept-Encoding: gzip\r\n");
   fputs($s, "Connection: close\r\n");
   fputs($s, "\r\n");
   fflush($s);
   
   // read headers
   while (false!==($line=fgets($s, 65536))) {
      $line = chop($line);
      if (strlen($line)==0) break;     
      echo $line, "\n";
   }

   // read body here
   // ,,,,
   fclose($s);


} // else connection refused



?>



this is very similar to your suggestion via telnet of course Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Sat Apr 26, 2003 12:22 pm    Post subject: Reply with quote

unfortunately, ob_gzhandler is broken in 4.3.1 :/
Back to top
View user's profile Send private message
dthought
Smarty Regular


Joined: 21 Apr 2003
Posts: 55
Location: Melbourne, Australia

PostPosted: Sun Apr 27, 2003 10:30 am    Post subject: Re: $smarty ->display(tpl,cacheid,compress) Reply with quote

hinrichs wrote:
It is great when developers can make pages (templates) without having to worry about adding extra spaces, tabs, etc. to make it easier to maitain, in its effect on bandwidth.

That sounds like you also want to strip whitespace before you send the page...
Back to top
View user's profile Send private message Visit poster's website
hinrichs
Smarty Rookie


Joined: 24 Apr 2003
Posts: 28
Location: San Francisco, CA

PostPosted: Mon Apr 28, 2003 4:53 pm    Post subject: stripping whitespace Reply with quote

This was actually what I was originally talking about, as I was unaware that the gzip option was an option whose time has come--I thought it might alienate some users. Sounds however like it has developed to the point it will work for almost everyone.

I did discover the trimwhitespace output filter. This is very cool and a no-brainer for cached pages. I just put this in my smarty include:

Code:
      if ($GLOBALS['COMPRESS']){
         $this->load_filter('output','trimwhitespace');
      }


However if the page happens not to be cached, I suppose you might need to weigh the overhead of trimming the whitespace from the page versus the overhead of sending the original whitespace over the network. This seems like the realm of black art and I'm not sure I want to embark on such tests.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP