Monday, August 27, 2012

Simple Template Class-Example

Simple Template Class-Example

<?php

class template
{
   /*
     DESCRIPTION:
     A VERY simple template class. No caching or advanced functionality.
     It simply opens a file and replaces any variable in the file with a syntax of
     '{VARIABLE}' It is primarily designed to keep layout HTML out of php files. Right now it is case sensitive but in future versions i will probably put in a setting for that.
    
I will probably update it with more functionality in the future but for now it could be extended by child classes etc.
    
     */
     var $tpl_dir = 'templates';
   var $tpl_vars = array();
    
    
     // PRIVATE:
    
     function repvar($name, $value, $string)
   {// Function to replace a variable
      return str_replace('{'.$name.'}', $value, $string);
   }
         
     // PUBLIC:    
      
   function addvar($name, $value)
     {// Add variable to be replaced in a template
        $this->tpl_vars[$name] = $value;
     }

   function display($page)
     {
        $fhandle = file_get_contents($this->tpl_dir.'/'.$page);
            foreach($this->tpl_vars as $name=>$value)
            {
               $fhandle = $this->repvar($name, $value, $fhandle);
            }
            return $fhandle;
     }
}

/*
//EXAMPLE
$tpl = new template;
$tpl->addvar('TITLE', 'Template Test');
$tpl->addvar('CONTENT', $content);
$tpl->addvar('MENU', 'Template Test');
echo $tpl->display('main.htm');
*/
?>

No comments:

Post a Comment

Labels

AJAX (1) Answers (1) Apache (1) Array (16) bug (1) C (1) C++ (1) Calendar (1) Class (1) Commands (1) Cookies (2) Database (2) Date (7) Days (1) explode (1) File Upload (1) FILES (1) firewall (1) fix (1) Functions (26) GET (1) GMT (1) JavaScript (2) localhost (1) Mail (1) Menu (1) MYSQL (13) PERL (1) PHP (36) php.ini (1) POST (1) preg_match (1) Questions (1) Script (1) SMS (2) Solution (1) String (1) Time (5) Time Zone (1) Vista (1) Wamp Server (1) windows 7 (2) XML (1)

Popular Posts

Popular Posts