X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Changing design (https://forum.x-cart.com/forumdisplay.php?f=51)
-   -   How and where do I use a Smarty Output filter in X-Cart? (https://forum.x-cart.com/showthread.php?t=188)

minorgod 10-04-2002 07:39 AM

How and where do I use a Smarty Output filter in X-Cart?
 
I'm building a URL-rewriter for my cart to make it search-engine-friendly. I'm making an outputfilter/plug-in to take care of the actual URL re-writing so I don't have to modify anything other than the templates (hopefully) and then add a small bit of PHP code to all the PHP pages to accept get variables in the format:

name/value/name/value/name/value

rather than

name=value&name=value&name=value

Is there anything anyone can tell me about using Smarty Output Filters? Do I just need to register the output filter once in one of my main templates or do I need to include it just before and explicit
Code:

$smarty->display();
call?

ATKOgirl 10-04-2002 08:27 AM

Hi minorgod,

I hope you'll share your rewriter with the forum when you're done!

ATKOgirl

B00MER 10-04-2002 08:36 AM

Minorgod, I'd be interested in the steps you took for such, as I'm intersted the methods taken instead of using apache's mod_rewrite.

Good luck to you! 8)

minorgod 10-04-2002 01:09 PM

preliminary test results
 
So far I've managed to build a url rewriter plug-in that finds any urls in the pending Smarty output and fills them with a test URL in the format http://someurl. I've run into a snag doing a sub-expression match/string replacement to replace all the special url parameter delimiters with slashes. I'm a regex newbie and this is damn complicated! Any regex gurus out there that can lend a hand fixing this regex? Here's an example of the test code that isn't working right...

Code:

<?php //preg_test


$source = "this is some html in unformatted format with a <a href=\"directory/link.php?parameter1=parameter1value&parameter2=parameter2value\">link</a>";

echo htmlspecialchars($source),"

";

$source = preg_replace("/(a href=\")([^\"]+)(\")/", "'$1'.'str_replace(array('&','?','+','='),'/','$2')'.'$3'", $source);

echo htmlspecialchars($source),"

";

$samplestring = "astring";

echo "now here's a simple astring replacement just to make sure I'm doing that part right..
";
echo $samplestring," becomes ";
echo str_replace(array('a','s','t','r','i','n','g'),"s",$samplestring);

?>


Hope someone knows what I'm doing wrong here, cuz I don't! I'll be working on this over the weekend so please post if you know the answer. I'll release a small tutorial once I get everything working right (assuming it doesn't require hand-coding on many php pages in which case I may not write a tutorial).

B00MER 10-04-2002 01:17 PM

Good for testing out regexpr, helped me out on alot of regular expressions in perl:
http://www.savarese.org/oro/demos/OROMatcherDemo.html

minorgod 10-04-2002 04:56 PM

I FOUND it!
 
After over 8 hours of testing various combinations of regular expression syntax, I have managed to get the following to work for changing the URL parameter syntax in a link to use slashes instead of standard URL parameter syntax.....

Code:

$source = preg_replace("/(a href=)(\")([^?]+)([^\"]+)(\")/e", "'$1$3'.str_replace(array('&','?','+','='),'/','$4')", "$source");

Hope this saves someone 8 hours. It turns out that the main stumbling block was the use of the /e flag at the end of the preg pattern. Without that little "e" after the pattern, PHP wouldn't execute the substring search on the 4th substring that contained the parameters needing slash replacement. Now it does. Now on with the search-engine friendly cart mod!

minorgod 10-07-2002 07:10 AM

disregard that last post
 
That last post was on the right track, but the final version required much more work. All the bugs still aren't worked out. The module now rewrites all the links on the page using slashes as separators, but now when the page loads, it immediately does a full store search and lists all the items in my store. Not sure why this is happening except that I suspect the search form is being activated by some variable being set when Smarty renders the page the first time, before the output filter has a chance to rewrite the links. Not sure if this is the problem yet. Can anyone think of a reason my customer/home.php would instantly reload with a full store search?

minorgod 10-07-2002 07:58 AM

Eureka!
 
Okay, below is a new Smarty Output Filter I wrote to rewrite all the links on a page to be search-engine friendly. HOWEVER, you cannot simply drop this in your Smarty Plug-ins folder and be done. If you rewrite all your page links using this plug-in, then you'll still need to alter your application logic to handle GET varables differently, so you can parse them from the PATH_INFO rather than from the _GET or HTTP_GET_VARS array. Here is the code to make your own Smarty Plug-in....

Code:

<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:    outputfilter.make_searchable.php
 * Type:    outputfilter
 * Name:    make_searchable
 * Version:  1.0
 * Date:    October 7, 2002
 * Purpose:  To rewrite all the links on your page to a search engine friendly
                          format. If you use this filter, you will also need to change the logic
                        of your php source to handle GET variables differently. This script attempts
                        to help you do this by filling in any unset GET variables with the value
                        "undefined" so you have an easier time matching up the variables with their
                        values.
 * Install:  Drop into the plugin directory, then in your php pages call
 *          $smarty->load_filter('output','make_searchable');
 *          just before you call your display such as $smarty->display("customer/home.tpl");
 * Author:  Brett R. Brewer < brett@brettbrewer.com, brett@realestate-school.com >
 * -------------------------------------------------------------
 */
 function smarty_outputfilter_make_searchable($source, &$smarty)
 {
 

//now lets build an array of all the link nodes in the source

// Pull out the links and replace them with markers for later use
    preg_match_all("!<a[^>]+>.*?</a>!is", $source, $match);
    $_anchor_tags = $match[0];
    $source = preg_replace("!<a[^>]+>.*?</a>!is",'@@@SMARTY:TRIM:HREF@@@', $source);

//now strip out the actual href values




//fix any unfilled query values by inserting the value as "undefined"

foreach($_anchor_tags as $curr_anchor){
       
//replace any hanging query arguement at the end of the string
        //which we can tell if it ends with =" or theres an =# or =&

        $curr_anchor=preg_replace("/(=\x22>)|(=\x27>)/","=undefined\">",$curr_anchor); //replaces dangling =
        $curr_anchor=preg_replace("/=#/","=undefined#",$curr_anchor); //replaces =# situations
        $curr_anchor=preg_replace("/=&/","=undefined&",$curr_anchor);  //replaces =& situations       
        $fixed_anchors[]=$curr_anchor;
}


//now strip out the actual href values including quotes, replacing any url parameter
//characters with slashes

foreach($fixed_anchors as $fixedanchor) {
        preg_match("!(\"|')([^\"']+)(\"|')!is", $fixedanchor, $url_value);
        $url_values[]=preg_replace("/[&=#?]+/","/",$url_value[0]);       
}

//now replace the old http value with the new slashed version
foreach($fixed_anchors as $key=>$value){
        $fixed_anchors[$key]=preg_replace("/(\"|')([^\"']+)(\"|')/","$url_values[$key]",$value,1);
}

//now replace the original anchor tags with the new ones...
foreach($_anchor_tags as $key=>$value){
        $_anchor_tags[$key]=$fixed_anchors[$key];       
}


// replace all the markers in the source code with the modified links

foreach($_anchor_tags as $curr_anchor) {
                $source = preg_replace("!@@@SMARTY:TRIM:HREF@@@!",$curr_anchor,$source,1);
}

//now return the modified source code

return $source;

}
?>


Please note, this code is not optimized. A couple of steps here could easily be reduced into a single step, but I needed the extra steps for easier debugging. If you modify this or discover bugs, please post them here. If you need to see the code that is being generated without rendering it in your browser, simply change the last line of this output filter to
Code:

return htmlspecialchars($source);
and it will fill your browser with source code rather than rendered code. Very helpful for debugging.

Below is a sample PHP script that you might use to grab the variable names and values from the URL on pages where you use this output filter, or target pages you might navigate to from a page using this output filter:

Code:

<?php
/*
A simple script to translate path-ized URL parameters back into variables.
*/


        //check for existence of $PATH_INFO
          if(isset($PATH_INFO)) {

        //create an array of the variable names and their values
              $vardata = explode('/', $PATH_INFO);

        //count the number of parameters
                $num_param = count($vardata);

        //if the number of parameters is even add an extra empty element
        //to simplify array traversal
                if($num_param % 2 == 0) {

                    $vardata[] = '';
                    $num_param++;
                }
        //now loop through the array and create a variable
        //with the correct name for each value and set the value
                for($i = 1; $i < $num_param; $i += 2) {

                    $$vardata[$i] = $vardata[$i+1];
                }
            }





?>


Love me.

B00MER 10-07-2002 11:21 AM

Excellent work :)

rrf 10-08-2002 03:33 AM

X-Cart: Static Catalog
 
Actually, the next version of X-Cart will have a "Static Catalog" feature built-in, which will make it more search engine-friendly.

The cart will be able to generate a HTML version of the catalog automatically. All "dynamic" buttons like "Add to cart", "View cart", "Login", etc. will be linked to the standard X-Cart PHP pages.


All times are GMT -8. The time now is 07:03 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.