...

View Full Version : how to parse template file ??



PHPycho
08-29-2007, 12:25 PM
Hello forums !!
I have some file named "test.tpl"
whic has following contents
"Hello %name% your passwrod is %passwrod%"
I would like to call this file and replace those values within % with dynamic values(may also {}).
Any comments and suggestions are warmly welcome.
Thanks in advance to all of you

matak
08-29-2007, 02:14 PM
sams publishing quick template class, looks something like this. i don't know is it ok to post it here


<?php
class quick_template {

private $t_def;

public function parse_template($subset = 'main') {

$noparse = false;
$content = "";
$temp_file = $this->t_def[$subset]['file'];

if(isset($temp_file)) {

if(strlen($temp_file) > 6) {
substr($temp_file, strlen($temp_file)-6);
}

if(strcasecmp($ext, ".thtml") != 0) {
$noparse = true;
}

if(!$fr) {
$content = "<!-- Error loading '$temp_file' //-->";
} else {
$content = fread($fr, filesize($temp_file));
}

@fclose($fr);

} else {

if(isset($this->t_def[$subset]['content'])) {
$content = $this->t_def[$subset]['content'];
} else {
$content = "<!-- Content for '$subset' not defined //-->";
}

}

if(!$noparse) {

$content=preg_replace("/\%([A-Z]*)\%/e",
"quick_template::parse_template(strtolower('$1'))",
$content);
}

return $content;

}

function __construct($temp='') {

if(is_array($temp)) $this->t_def = $temp;

}
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum