All pastes #444737 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-04-17 14:00 UTC
rendered paste body
class template
{
 var $tpl_vars = array();

    function set_to($var_array)
    {
    
        if (!is_array($var_array))
        {
            trigger_error('Error: template::assign() argument must be an array', E_USER_ERROR);
        }
        
        $this->tpl_vars = $var_array;
        
    }

    function parse($file)
    {
    
        if (!is_file($file))
        {
        
            trigger_error('Error: template::parse() - "' . $file . '" does not exist or is not a file', E_USER_ERROR);
        
        }
        
        $tpl_content = file_get_contents($file);
        
        foreach ($this->tpl_vars AS $var => $content)
        {
        
            $tpl_content = str_replace('{' . $var . '}', $content, $tpl_content);
            
        }

        return $tpl_content;
    }

    function display($file)
    {
    
        echo $this->parse($file);
        
    }
}


USAGE--------

$tpl = new template;

$tpl->set_to(array(
    'title'   => 'TestTemplateSYS'
    'slogan' => 'do i really care?'
    ));
    
DEFINE('THEME_NAME', 'default');

//$tpl->display('tpl/' . THEME_NAME . '/header.php');