<?php

class LensTemplate
{
	var $srcArray, $dstArray;
	var $prefix = '\{\$';
	var $suffix = '\}';
	var $flags = 'i'; // case-insensitive 
	var $unmatchedValue = '';
	
	function LensTemplate()
	{
		$this->srcArray = array();
		$this->dstArray = array();
	}
	
	function Assign($arr,$arr2=null)
	{
		if (is_array($arr)) {
			$prefix = $this->prefix;
			$suffix = $this->suffix;
			$flags = $this->flags;
			foreach($arr as $k => $v) {
				$this->srcArray[] = '/'.$prefix.$k.$suffix.'/'.$flags;
				$this->dstArray[] = $v;
			}
		} else {
			$this->srcArray[] = '/'.$this->prefix.$arr.$this->suffix.'/'.$this->flags;
			$this->dstArray[] = $arr2;
		}
	}
	
	function Fetch($tplText)
	{
		$txt = preg_replace($this->srcArray,$this->dstArray,$tplText);
		// Remove all unassigned variables...
		return preg_replace('/'.$this->prefix.'[a-z0-9_\-\.]*'.$this->suffix.'/'.$this->flags,
			$this->unmatchedValue,$txt);
	}
	
	function Display($tplText)
	{
		print $this->Fetch($tplText);
	}
	
};

$t = new LensTemplate();
$t->Assign(array('mailto'=>'jlim@natsoft.com.my', 'subject'=>'Hello'));
$t->Assign('HI','Hello body');
print_r($t);
$t->Display("<pre>
{\$MAILTO}
subject: {\$subject}
{\$not used } {\$HI}
");
?>