#native_company# #native_desc#
#native_cta#

Cached Dynamic Modules Page 4

By JP
on July 30, 2000

The parse_it function scans the string to see if any tag occurs, and if it does, break
down the tagname in $fun and its optional parameters into the associated array $arglist.

<?php

function parse_it ($str) {

    global 
$loaded;

    if (eregi ("<[Mm][Yy]-([A-Za-z0-9]*) ([^>]*)"$str$regs)) {

        
$tag $regs[1];

        if (!
$loaded[$tag]) {

            include 
"res/$tag/$tag.php";

            
$loaded[$tag] = 1;

        }

        $fun "handle_$tag";

        
$list explode (" "strtolower ($regs[2]));

        
$cache_file "cache/$tag";

        for ($i 0$i count ($list); $i++) {

            if (
$argname strtok ($list[$i], "=")) {

                
$arglist[$argname] = strtok ("=");

                if (
$argname != "cache") {

                    
$cache_file .= "_" $argname "=" $arglist[$argname];

                }

            }

        }

        $buf "<!-- $tag start here //-->n";

        
$buf .= $fun ($arglist);

        
$buf .= "n<!-- $tag ends here //-->n";

        return 
$buf;

    } else {

        return 
$str;

    }

}

?>



This function calls the function $tag directly in the file included from res/$tag/$tag.php
and uses the output to return to the parse main loop.
As you notice, the variable $cache_file is also being built pointing to cache/$tag…
where … is a string combining all parameters to form a unique cache entry for this tag. To
actually use the cache, we must add some code between the end of the for loop and the creation of $buf:

<?php

$read_cache 0;

$write_cache 0;

if (!(isset (
$arglist["cache"]) && ($arglist["cache"] < 10))) {

    
$write_cache 1;

    if (
file_exists ($cache_file)) {

        if (!isset (
$arglist["cache"])) {

            if ((
filemtime ($cache_file) + $default_cache_time) > date ("U")) {

                
$read_cache 1;

                
$write_cache 0;

            }    

        } else {

            if ((
filemtime ($cache_file) + $arglist["cache"]) > date ("U")) {

                
$read_cache 1;

                
$write_cache 0;

            }

        }

    }

}

?>