Smarty.php Mod to Remove all UN-NECESSARY White Space
Smarty has an included filter that you can implement quite simply by adding one line of code to your SMARTY.PHP file.
Add the following line to the file just below the ImagesDir and SkinDir assign statements near the end: Code:
$smarty->load_filter("output","trimwhitespace"); It safely removes all whitespace from the generated HTML just prior to sending the page to the client - increases speed dramatically by reducing the overall size of the file. It works great and you will have no more un-necessary blank lines or spaces. Check it out before and after by using View, Source in your browser on any X-Cart page - the difference is easily noticable. Al Ponte |
Have you tested this will all the JavaScript that is in X-Cart? Some JS does not function wel after removing whitespace....
|
Ok... I see... it is actually leaving the Javascript alone. Much different than {strip} tags. Interesting....
|
Nifty trick, tested it with 3.5.x works like a charm. You know what version this was added to smarty :?:
|
Thanks Adian009,
Works great in 3.5.8! Before I had 417 lines of code in one of my pages, now I have 404 :wink: I was looking for this function, even wanted to use external editor. |
Very good discovery !! Its speeded up my site
|
Did anyone find that it stripped out the spaces in the e-mail templates too?
|
Quote:
Anyone know if there is a way to prevent this by perhaps putting certain tags in the email templates that prevent them from having the spaces stripped out? One problem I found with using {strip} tags is that two Smarty tags with a space between them will be butted up against each other, and so to maintain the spacing I had to put "" between them. Does this problem happen with this Smarty modification? :? |
Replies to All
Well, you use {literal}{/literal} with JavaScript and it won't be touched. I have been using this for 3 months without issue.
This will only strip whitespace from templates being sent to a client - not email as far as I know. --al |
Some email templates are used for display in the browser.
I guess I will just stick with adding {strip} tags to various documents until I get this worked out. |
Measuring Impact of This
Does anyone have any ideas or interest in measuring the impact of triming the whitespaces?
Personally, I do - the only way I can think of to measure it would be to create a script that accesses specific pages in succession. Run the script with whitespaces then trimming them and compare the average Bandwidth and Response time. Anyone have any other ideas? I am working full-time at a client site who is a large ASP so we I have access to sophisticated web testing/stress testing software...just don't know what to test. --al |
Quote:
What the addition to Smarty will do is change this: Code:
<head> To this: Code:
<head> It is not going to strip out the \n commands (New Line) like the {strip} tag does. So, it is not going to shove all the email data on one line. As for measuring impact, that is easy. If you strip out the extra blank lines from an HTML file, then you decrease file size, thus making the download time of the HTML shorter. |
So is this safe to use?
I am running 3.4.14 and most of my invoice templates are in html. I've tried following this thread but the concensus isn't clear to me. |
So I just went and added it in. Haven't tested my invoices yet, but this is like VIAGRA for your store. Big time.
|
UPDATE.
Still working great EXCEPT it isn't playing nice with my plain text emails. Particularily the company signature and the order template. All of the blank lines between product details are gone and the products have no spaces between them. example Quote:
Should look like Quote:
and my email sig is just one run on line. |
Quote:
You ain't kidding :lol: Thanks for the tip Adian009 ;) |
Will this work for Smarty-2.5.0 ?
|
Quote:
Yes, that is what I have and it works! |
If I enclose my email tpls in {literal} tags should I be ok?
|
Well, here is what i get:
If i enclose my plain text email *.tpl in {strip}{/strip} the result is printed out in one line. Enclosing email in {literal}{/literal} don't parse code and just prints it out all code included between {literal} hmm :roll: |
For your plain emails, put all of your extra spaces and newlines in the literal tags. Not the whole template.
|
Does this remove the spaces between the vertmenu boxes as well, or just items within the main area? So will "Your Cart" be touching "Authentication", or will they still have a small space in between them?
|
Quote:
No, This doesn't relate to spacing vertical or horizontal that the browser will interrupt, to see what it does, disable the added code, view the source of your store, keep it open, then readd the code, and refresh your store, and view the source again. You'll see what it does ;) |
Quote:
What a PITA. I don't see me going through every single template for that. :P |
Quote:
Well, then I suggest you do the sensible thing and switch to HTML email. :P :wink: What does PITA stand for anyway?? :roll: |
Pain in the @$$. :)
Not all of my customers can receive html mail. Do you recommend that I ignore them? |
Hmm...maybe there should be an option for individual customers to receive the email in HTML or plain text when they sign-up. Anybody got an idea how to do this? :?
Use a text editor that supports regular expressions to wrap all of the newlines or spaces with literal tags. Use the following for the search: Code:
'/([\n\r\f\t ]+)/' Use this for the replace: Code:
{literal}\1{/literal} EditPad is really good for this. If you use PHP for the replacement, make sure you use \$1 instead of \1 Let me know if this works for you! PS: Don't forget to back-up those files first! |
Update on that regular expression. Try using /([ ]{2,}|[\n\r\f\t]+)/ instead as this seems to actually work. The previous regex I posted fubars the plain text templates by putting {literal} tags inside other Smarty tags.
This new one should allow you to easily upgrade your plain text templates and still use the Smarty mod posted here. :wink: |
NuAlpha,
I can't get your solution to work, so I tried various methods of your code, this is what I did with one of my mail templates: Code:
{literal}\1{/literal} but this is what I ended up with: \1 Dear Mr.adsfe21 \1 \1 You have been registered at Your Company Name! Setting up a Your Company Name account entitles you to quick and easy online shopping. \1 With a Your Company Name account, you can easily track and manage your orders, update your information, add items to your wishlist and much more! \1 Below are your account details: \$1 Account Information: --------------------- Username: adsfe21@saol.net '/([\n\r\f\t ]+)/' Password: adsfe21 /([\n\r\f\t ]+)/ First Name: adsfe21 Last Name: adsfe21 /([ ]{2,}|[\n\r\f\t]+)/ E-Mail: adsfe21@saol.net |
I originally suggested using this and I guess people are benefiting - that is awesome!
I just want to interject - I have never had any issues using this script, even with my plain text emails?!? I just tested them again - still no problem...I wonder why? I will do more research to see if my implementation of this differs from everyone else's...I will let you all know, soon! --al |
xcell67, it looks as if spaces are literally being replaced by the regular expression itself! 8O
If you are using PHP, please post the exact preg_replace() code that you are using including the regular expression and replacement in that code. If you are using EditPad, select the regular expressions option in the search box and use the regex ([ ]{2,}|[\n\r\f\t]+) with the replace {literal}\1{/literal} |
I tried using dreamweaver to do it manually, I put {literal}, pressed enter twice to insert a blank line then ended it with {/literal}
I also tried your method with Editpad but still no luck =( One thing that did manage to work for me was putting <PRE> and ending with </PRE>. That allows the blank lines between text but I don't think its too customer friendly to have those tags in the mails. Can all you lucky people who have managed to get this to work post a sample tpl from your mail folder. Thanks. |
works well for me in 4.0.1 :lol:
|
You *must* place this call after $mail_smarty is instantiated - then it WILL NOT PARSE THE MAILS. :)
Here is my placement and all will be well! # # Smarty object for processing mail templates # $mail_smarty = $smarty; $smarty->load_filter("output","trimwhitespace"); # # WARNING : # Please ensure that you have no whitespaces / empty lines below this message. # Adding a whitespace or an empty line below this line will cause a PHP error. # ?> |
WHOOOOOO! I finally got my mail tpls to work with your solution kind sir.
Thank you and thank you to NuAlpha for helping me with his solution. This is a sweet mod. |
Quote:
That's great - glad to know I could help out! I wish I was monitoring this thread actively - I am now...hopefully it wasn't too late! --al ponte |
Works in 4.0.2, thank you. :)
|
Under PHP 4.3.10, I have found that unless I remove the line $smarty->load_filter('output','trimwhitespace'); from smarty.php then the entire Xcart load process will abort suddenly for some unknown reason.
Has anyone else experienced this? |
This is a great MOD but any HTML invoices received by customers were blank.
Quote:
I tried adding $smarty->load_filter('output','trimwhitespace'); and it caused problems with images on the website. As a result I removed the mod for now until I can find a solution. Anyone have any ideas? |
Sweet
Thanks
|
All times are GMT -8. The time now is 06:50 AM. |
Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.