Zen Cart Logo
Forums / Templates, Stylesheets, Page Layout / A better way of dealing with large define() statements?

A better way of dealing with large define() statements?

Views: 851

Results 1 to 8 of 8
17 May 2011, 4:04 PM
#1
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

A better way of dealing with large define() statements?

I came across this tip on a site while checking a now obsolete thread. The site is about seven years old and mostly or completely irrelevant to current Zen Cart, but this seems like it could be useful and worth preserving. I haven't tried it yet.

A better way of dealing with large define() statements

Many language files for static pages have the basic format of:

<?php define('NAVBAR_TITLE', 'Page Title'); define('HEADING_TITLE', 'Page Heading'); define('TEXT_INFORMATION', 'Lots and lots of body text.'); ?>

This makes the TEXT_INFORMATION a b*tch to modify. Also if you're simply cutting and pasting a large amount of HTML, it means that you have to go through and make sure all of your single quotes and backslashes are escaped. Also if you're using an editor like Homesite, it won't show any color coding for any HTML tags you have. This sucks.

A better way to do it is like this:

<?php define('NAVBAR_TITLE', 'Page Title'); define('HEADING_TITLE', 'Page Heading'); ob_start(); ?>

Lots and lots of free body text.

<?php define('TEXT_INFORMATION', ob_get_contents()); @ob_end_clean(); ?>
17 May 2011, 6:34 PM
#2
coolcarpartsonline avatar

coolcarpartsonline

Totally Zenned

Join Date:
Feb 2008
Posts:
1,386
Plugin Contributions:
0

Re: A better way of dealing with large define() statements?

Nice tip. I can think of few pages that I can use this on.

Thanks.

17 May 2011, 10:12 PM
#3
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: A better way of dealing with large define() statements?

Use with EXTREME caution, esp if your site has gzip-compression enabled. Else you'll potentially end up conflicting with the existing output buffering built-in to ZC.

Far better to use your favorite HTML editor, and just copy-and-paste from that, escaping afterwards where necessary.

18 May 2011, 2:03 AM
#4
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: A better way of dealing with large define() statements?

Okay, I had looked at the PHP manual which mentioned a caveat to ob_start(), but that didn't seem to apply to this use. If there is already output buffering in ZC, I would not risk competing with it (even though the manual says multiple layers of ob_start() can nest).

19 May 2011, 6:36 AM
#5
website_rob avatar

website_rob

Inactive

Join Date:
Oct 2006
Location:
Alberta, Canada
Posts:
4,572
Plugin Contributions:
0

Re: A better way of dealing with large define() statements?

This post got me thinking as I've had the same type problem with certain code within define statements. Having a need to use the "heredoc" syntax in a recent site, I got to wondering if it could be used in this type situation. Turns out it can!

For proof of concept using PHP 5.3 and v1.3.9d on a Linux Server, I tested using this file:

includes/languages/english/custom/shopping_cart.php

I changed this:

define('OUT_OF_STOCK_CANT_CHECKOUT', 'original text here');

to this:

$var = '<bold style="color:blue">this is a previously declared variable value</bold>';

define('OUT_OF_STOCK_CANT_CHECKOUT', <<<stringtext

<div style="padding:10px; font-size:1.15em; color:green; background:wheat">First we provide a custom background color for part of this example. <br /> This sentence uses 'single quotes' and also uses "double quotes" without any <strong style="color:black">\</strong> or <strong style="color:black">/</strong> slashing and it makes no difference. <br />   star <strong>*</strong>   dollar sign <strong>$</strong>   comment <strong>#</strong>   >>> [<a style="color:purple" href="#" onmouseover="alert('Alert window works fine!')">Mouseover this link</a>] <<< <form> <input type="button" value="Click here to see what happens!" onclick="alert('This heredoc stuff could be handy.')"> </form> <br />

Watch out though if using $variables, which will be properly converted to their values.
<br />
(i.e. & #0036;var = $var) << must use the ASCII equivalent in the first var usage, forum won't let me code it that way

</div> stringtext );

Then added to my Shopping Cart, more of a product then what was available to see the results.

The above is just some rough, trial & error code but gives a good indication of how "heredoc" can be used; for this situation and others.

Along with what is mentioned about about $variables, I also noticed using /* */ worked as regular comment tags so be careful about using that type syntax. There are a few other caveats to using "heredoc" but it is easy to learn.

  • You can use anything you like; "stringtext" is just an example of a delimiter
  • You need to use <<< before the delimiter to tell PHP you want to enter heredoc mode
  • You can use your delimiter anywhere in the text, but not in the first column of a new line
  • At the end of the string and in the above type situations, just type the delimiter with no spaces around it
19 May 2011, 6:51 AM
#6
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: A better way of dealing with large define() statements?

Yes, heredoc syntax can be used. Use it sparingly. It should be considered an approach suitable only for "advanced" programmers. Newbs could shoot themselves in the foot unknowingly.

19 May 2011, 7:07 AM
#7
website_rob avatar

website_rob

Inactive

Join Date:
Oct 2006
Location:
Alberta, Canada
Posts:
4,572
Plugin Contributions:
0

Re: A better way of dealing with large define() statements?

DrByte:

Newbs could shoot themselves in the foot unknowingly.
They do that anyway, with .htaccess files and other ways. :smile:

Besides, from what was originally posted I doubt a Newbie would understand what is being discussed. No offensive meant to Newbies but everyone is at different levels of knowledge and experience and no matter how much we know... many people know more. :yes:

19 May 2011, 12:37 PM
#8
rstevenson avatar

rstevenson

Totally Zenned

Join Date:
Nov 2006
Location:
Dartmouth, NS Canada
Posts:
2,400
Plugin Contributions:
0

Re: A better way of dealing with large define() statements?

I'm not a newbie, and I have no idea what you're talking about. :blink:

Rob