X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (https://forum.x-cart.com/forumdisplay.php?f=20)
-   -   Smarty.php Mod to Remove all UN-NECESSARY White Space (https://forum.x-cart.com/showthread.php?t=7920)

Adian009 06-04-2004 09:40 PM

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

TelaFirma 06-05-2004 04:56 AM

Have you tested this will all the JavaScript that is in X-Cart? Some JS does not function wel after removing whitespace....

TelaFirma 06-05-2004 05:01 AM

Ok... I see... it is actually leaving the Javascript alone. Much different than {strip} tags. Interesting....

B00MER 06-08-2004 12:42 PM

Nifty trick, tested it with 3.5.x works like a charm. You know what version this was added to smarty :?:

FX 06-10-2004 07:42 PM

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.

carlisleglass 06-10-2004 11:37 PM

Very good discovery !! Its speeded up my site

PhilJ 06-11-2004 03:28 AM

Did anyone find that it stripped out the spaces in the e-mail templates too?

NuAlpha 06-12-2004 09:28 AM

Quote:

Originally Posted by PhilJ
Did anyone find that it stripped out the spaces in the e-mail templates too?


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? :?

Adian009 06-12-2004 02:57 PM

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

NuAlpha 06-12-2004 07:42 PM

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.

Adian009 06-13-2004 08:11 PM

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

TelaFirma 06-15-2004 03:53 AM

Quote:

Originally Posted by NuAlpha
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.


What the addition to Smarty will do is change this:

Code:

<head>

</head>

<body>


</body>


To this:

Code:

<head>
</head>
<body>
</body>


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.

adpboss 06-15-2004 07:23 AM

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.

adpboss 06-15-2004 07:31 AM

So I just went and added it in. Haven't tested my invoices yet, but this is like VIAGRA for your store. Big time.

adpboss 06-15-2004 11:08 AM

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:

SKU: 8801
Product: Blue Meteor Light Kit
Quantity: 1
Item price: $ 21.95
SKU: 3319
Product: 80mm Chrome Tribal Sun Laser Cut Fan Grill
Quantity: 1
Item price: $ 6.95

Should look like

Quote:

SKU: 8801
Product: Blue Meteor Light Kit
Quantity: 1
Item price: $ 21.95

SKU: 3319
Product: 80mm Chrome Tribal Sun Laser Cut Fan Grill
Quantity: 1
Item price: $ 6.95

and my email sig is just one run on line.

Emerson 06-15-2004 11:21 AM

Quote:

Originally Posted by adpboss
...this is like VIAGRA for your store. Big time.


You ain't kidding :lol:

Thanks for the tip Adian009 ;)

Jon 06-15-2004 04:05 PM

Will this work for Smarty-2.5.0 ?

Emerson 06-15-2004 04:07 PM

Quote:

Originally Posted by Jon
Will this work for Smarty-2.5.0 ?


Yes, that is what I have and it works!

adpboss 06-15-2004 04:09 PM

If I enclose my email tpls in {literal} tags should I be ok?

FX 06-17-2004 11:05 PM

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:

NuAlpha 06-18-2004 06:07 AM

For your plain emails, put all of your extra spaces and newlines in the literal tags. Not the whole template.

Ryano 06-24-2004 08:58 AM

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?

B00MER 06-24-2004 11:10 AM

Quote:

Originally Posted by Ryano
Does this remove the spaces between the vertmenu boxes as well, or just items within the main area?


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

adpboss 06-24-2004 04:09 PM

Quote:

Originally Posted by NuAlpha
For your plain emails, put all of your extra spaces and newlines in the literal tags. Not the whole template.


What a PITA. I don't see me going through every single template for that. :P

NuAlpha 06-24-2004 04:18 PM

Quote:

Originally Posted by adpboss
What a PITA. I don't see me going through every single template for that. :P


Well, then I suggest you do the sensible thing and switch to HTML email. :P :wink:

What does PITA stand for anyway?? :roll:

adpboss 06-24-2004 04:27 PM

Pain in the @$$. :)

Not all of my customers can receive html mail. Do you recommend that I ignore them?

NuAlpha 06-24-2004 04:45 PM

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!

NuAlpha 06-28-2004 06:14 PM

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:

xcell67 08-11-2004 02:52 PM

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}


{$lng.lbl_dear} {$userinfo.title} {$userinfo.firstname} {$userinfo.lastname},
{literal}\1{/literal}

{literal}\1{/literal}
{$lng.eml_you_have_been_registered} at {$config.Company.company_name}!



Setting up a {$config.Company.company_name} account entitles you to quick and easy online shopping.
{literal}\1{/literal}
With a {$config.Company.company_name} account, you can easily track and manage your orders, update your information, add items to your wishlist and much more!


{literal}\1{/literal}
Below are your account details:
{literal}\$1{/literal}


Account Information:

---------------------

{$lng.lbl_username}:    {$userinfo.login}
{literal}'/([\n\r\f\t ]+)/'{/literal}
{$lng.lbl_password}:    {$userinfo.password}

{literal}/([\n\r\f\t ]+)/{/literal}

{$lng.lbl_first_name}:  {$userinfo.firstname}
{literal} {/literal}
{$lng.lbl_last_name}:    {$userinfo.lastname}
{literal}/([ ]{2,}|[\n\r\f\t]+)/ {/literal}
{if $userinfo.company}{$lng.lbl_company}:      {$userinfo.company}


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

Adian009 08-11-2004 02:57 PM

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

NuAlpha 08-11-2004 03:05 PM

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}

xcell67 08-11-2004 10:39 PM

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.

TonyD 08-12-2004 10:57 AM

works well for me in 4.0.1 :lol:

Adian009 08-12-2004 04:39 PM

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.
#
?>

xcell67 08-12-2004 06:53 PM

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.

Adian009 08-12-2004 07:59 PM

Quote:

Originally Posted by xcell67
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.


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

bluecat 08-22-2004 01:01 PM

Works in 4.0.2, thank you. :)

NuAlpha 01-09-2005 09:21 AM

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?

CopperB 04-16-2005 09:41 PM

This is a great MOD but any HTML invoices received by customers were blank.

Quote:

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.


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?

heartsfire 04-17-2005 12:27 PM

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.