Jump to content

template class


RoseauRam

Recommended Posts

I cant seem to get ahold of the person that created this class, as I am having some issues getting it to work.

 

class TplClass
{
var $_storage;
var $_output;
function tpl_get_block($str,$call)
{
	if (preg_match('/tpl:/',$str)) 
	{
		$arr = explode(":",$str);					
		$arr = explode("]",$arr[1]);				
		parse_str($arr[0],$out);					
		return $out[$call];
	} 
	else 
	{
		return false;
	}
}
function tpl_load($file)
{
global $id;

if (!file_exists($file)) 
{
	exit('template ['.$file.'] not found');
} 
else 
{
	$content = file($file);
	foreach ($content as $key => $value) 
	{
		if (!$this->tpl_get_block($value,'id')) 
		{
			$this->_storage[$id][] = $value;
		}	 
		else 
		{		
			$id = $this->tpl_get_block($value,'id');
		}
	}
}
}
function tpl_parse($block,$args='')
{	
	if (!is_array($this->_storage)) 
	{		
		exit('no template loaded');
	} 
	else 
	{    
		if (!array_key_exists($block,$this->_storage)) 
		{
			exit('[tpl:id='.$block.'] not defined by this template');
		} 
	else 
	{
		$tmp = implode('',$this->_storage[$block]);
		if (is_array($args)) 
		{
			foreach ($args as $key => $value) 
			{
				$tmp = str_replace('{'.$key.'}',$value,$tmp);
			}
		}
		$tmp = preg_replace('/{.*?}/','',$tmp);
		$this->_output .= $tmp;
	}
	}
}
function tpl_render()
{
	print $this->_output;
}
}
?>

 

as you can see its a template engine.

 

This is what I have tried

 

$TPL = new TplClass;
$TPL->tpl_load("ramletter.tpl");
$TPL->tpl_parse("USERNAME", $userdata['user_name']);
$TPL->tpl_render();

 

ramletter.tpl

Welcome [tpl:USERNAME] To the home of the Roseau Rams

 

error message

[tpl:id=USERNME] not defined by this template

 

so I am sure I am missing something that is small, but if anyone could point out the things I am doing wrong I would appreciate it.

 

Link to comment
https://forums.phpfreaks.com/topic/104064-template-class/
Share on other sites

after looking at the code again, I noticed something, the ID reminded me of styles. so When I changed the above to this.

 

code to load and display tpl file

$TPL = new TplClass;
$TPL->tpl_load("ramletter.tpl");
$TPL->tpl_parse("WELCOME", array(USERNAME => $userdata['user_name']));
$TPL->tpl_render();

 

tpl file

[tpl:id=WELCOME]
Welcome {USERNAME} To the home of the Roseau Rams

 

This was the solution that I needed.. Well now its shared with everyone :)

Link to comment
https://forums.phpfreaks.com/topic/104064-template-class/#findComment-532856
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.