Snippet
<?php
/**
wrote by dave husk, easyphpscripts.com
free software, no warantees.
$tags1 = array("TESTa1"=>"Testing once array","TESTa2"=>"<b>testing twice array</b>");
$tags = array("TEST3","TEST4");
$text = array("Testing once 3","testing twice 4");
//echo "<br>test brackets<br>";
$string = "
<br>test string<br>
{TEST1} <b>{TEST2}</b>
<br>
{TIME}";
echo "<br><br>test class<br>";
$temp=new EasyPHPTemplate();
$temp->load("test.tmp",$string);
//$temp->encase("%");
$temp->replace("TIME",date("H-i-s"));
$temp->replace("TEST1","Testing once");
$temp->replace("TEST2","testing twice");
$temp->replace($tags1);
//change the type of template tags.
$temp->encase("[","]");
$temp->replace($tags,$text);
$temp->render();
*/
class EasyPHPTemplate {
public $template;
private $encased_l = "{";
private $encased_r = "}";
function load($filepath,$string='') {
$f= fopen($filepath, 'r');
}
if($string) {
$this->template .= $string;
}
}
function encase($var,$val='') {
if($val != ''){
$this->encased_l = $var;
$this->encased_r = $val;
} elseif($val == ''){
$this->encased_l = $var;
$this->encased_r = $var;
}
}
function replace($var,$val='') {
$this-> template = str_replace($this-> encased_l. $var. $this-> encased_r, $val, $this-> template);
} elseif($val == '' && is_array($var)) {
foreach($var as $var => $val)
$this-> template = str_replace($this-> encased_l. $var. $this-> encased_r, $val, $this-> template);
foreach($var as $idx => $varable)
$this-> template = str_replace($this-> encased_l. $var[$idx]. $this-> encased_r, $val[$idx], $this-> template);
}
}
function render() {
echo("{$this->template}");
}
}
?>
Copy & Paste
|