Klopfstock: easytemplate

Beitrag lesen

Ist es diese Klasse?

class easyTemplate {     /*      * Public variable: last error message      */

var $error = "";

/*      * Private variables      */

/*      * The tag values pairs, as defined by assign()      */

var $tags = array();

/*      * The trigger value pairs, as defined by trigger()      */

var $triggers = array();

/*      * The template file      */

var $template_file = "";

/**      *      * Constructor - assigns template_file to the class.      *      * @param string $template_file Name (including path) of the file      *                              that contains the template.      *      * @return nothing      */

function easyTemplate($template_file)     {         /*          * Basic check whether the template file exists or not.          */

if(!file_exists($template_file))         {             $this->error = "The template file $template_file does not exist.";         }

/*          * Assigns template file          */

$this->template_file = $template_file;     }

/**      *      * Assigns a value for a template tag.      *      * @param string $tag     Tag that has to be substituted (assigned)      * @param string $value   Value hat has to be assigned to the tag      *      * @return int $errcode   Error code (see global errors)      */

function assign($tag, $value)     {         /*          * bails out in case that tag is empty          */

if (empty($tag))         {             $this->error = "Tag name is empty";             return(false);             exit;         }

/*          * Assigns value to tag          */

$this->tags[$tag] = $value;         return(true);     }

/**      *      * Assigns a value for a trigger tag.      *      * @param string $tag     Tag that has to be triggered      * @param string $value   1 - enable, 0 - disable      *      * @return int $errcode   Error code (see global errors)      /     function trigger($tag, $value)     {         /          * bails out in case that tag is empty          */

if (empty($tag))         {             $this->error = "Tag name is empty";             return(false);             exit;         }

/*          * Assigns value to tag          */

$this->triggers[$tag] = $value;         return(true);     }

/**      *      * Returns the parsed template as string.      *      * @param string $contents Contents that are returned      *      * @return int $errcode    Error code (see global errors)      */

function easy_parse(&$contents)     {         /*          * Reads in template file, suppresses error messages          */

$contents = @implode("", (@file($this->template_file)));

/*          * Loops through all assigned tag-value pairs          */

while(list($key, $value) = each($this->tags))         {             /*              * Constructs the template tag name              */

$tag = '{'.$key.'}';

/*              * checks if tag is in template              */

if(!strstr($contents, $tag))             {                 $this->error = "Tag $tag not found in template ".                                $this->template_file.".";

return(false);             }

/*              * Replace the template tag with the respective value              */

$contents = str_replace($tag, $value, $contents);         }

/*          * Loops through all assigned triggers          */

while(list($key, $value) = each($this->triggers))         {             /*              * Constructs the template tag name              */

$tag = '{TRIGGER_OPEN_'.$key.'}';

/*              * checks if tag is in template              */

if(!strstr($contents, $tag))             {                 $this->error = "Tag $tag not found in template ".                                $this->template_file.".";

return(false);             }

/*              * Replace the trigger with the respective value              */

if($value)             {                 /* trigger is set, don't comment out /                 $contents = str_replace($tag, "", $contents);             }             else             {                 / trigger is not set, comment it out */                 $contents = str_replace($tag, "<!--", $contents);             }

/*              * Constructs the template tag name              */

$tag = '{TRIGGER_CLOSE_'.$key.'}';

/*              * checks if tag is in template              */

if(!strstr($contents, $tag))             {                 $this->error = "Tag $tag not found in template ".                                $this->template_file.".";

return(false);             }

/*              * Replace the trigger with the respective value              */

if($value)             {                 /* trigger is set, don't comment out /                 $contents = str_replace($tag, "", $contents);             }             else             {                 / trigger is not set, comment it out */                 $contents = str_replace($tag, "-->", $contents);             }         }

return(true);     }

/**      *      * Parses and prints the current template.      *      * @return int $errcode    Error code      */

function easy_print()     {         /*          * Parses the template          */

$ret = $this->easy_parse($contents);

if($ret != true)         {             return($ret);         }         else         {

/*              * Outputs the parsed template              */

print($contents);             return(true);         }     }

/**      *      * Parses and returns the current template.      *      * @return string $contents      */

function easy_return()     {         $ret = $this->easy_parse($contents);

if($ret != true)         {             return($ret);         }         else         {             return($contents);         }     } }

?>