Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2005
    Location
    Upstate NY
    Posts
    22,010
    Plugin Contributions
    25

    Default 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(); ?>

  2. #2
    Join Date
    Feb 2008
    Posts
    1,336
    Plugin Contributions
    1

    Default 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.

  3. #3
    Join Date
    Jan 2004
    Posts
    66,364
    Blog Entries
    7
    Plugin Contributions
    274

    Default 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.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  4. #4
    Join Date
    Jul 2005
    Location
    Upstate NY
    Posts
    22,010
    Plugin Contributions
    25

    Default 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).

  5. #5
    Join Date
    Oct 2006
    Location
    Alberta, Canada
    Posts
    4,571
    Plugin Contributions
    1

    Default 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 />
    &nbsp; star <strong>*</strong> &nbsp; dollar sign <strong>$</strong> &nbsp; comment <strong>#</strong> &nbsp; >>> [<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
    Last edited by Website Rob; 19 May 2011 at 07:42 AM.

  6. #6
    Join Date
    Jan 2004
    Posts
    66,364
    Blog Entries
    7
    Plugin Contributions
    274

    Default 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.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  7. #7
    Join Date
    Oct 2006
    Location
    Alberta, Canada
    Posts
    4,571
    Plugin Contributions
    1

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

    Quote Originally Posted by DrByte View Post
    Newbs could shoot themselves in the foot unknowingly.
    They do that anyway, with .htaccess files and other ways.

    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.

  8. #8
    Join Date
    Nov 2006
    Location
    Dartmouth, NS Canada
    Posts
    2,378
    Plugin Contributions
    0

    Default 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.

    Rob

 

 

Similar Threads

  1. v150 Dealing with custom pruducts
    By easyonthetush in forum Setting Up Categories, Products, Attributes
    Replies: 1
    Last Post: 31 Aug 2012, 03:59 AM
  2. v139h Large Store with large # of Categories - better to Link or Copy Products?
    By Emily1972 in forum Basic Configuration
    Replies: 1
    Last Post: 31 May 2012, 11:52 AM
  3. Dealing with quantity pricing?
    By craigtrombly in forum Setting Up Categories, Products, Attributes
    Replies: 1
    Last Post: 6 Dec 2010, 07:12 PM
  4. Dealing with multiple currencies
    By nev in forum General Questions
    Replies: 0
    Last Post: 29 May 2008, 06:35 PM
  5. Sorting question dealing with numbers.
    By redrumloa in forum Templates, Stylesheets, Page Layout
    Replies: 7
    Last Post: 13 Aug 2006, 02:42 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR