Ticketa
Registered
Някой можели да ми помогне да направим на тоя Template Parser един Looping.
Т.е. искам примерно в темплейт файла да има:
<html>
<head>
<title>title</title>
</head>
<body>
<h1>{COMMENTS_PAGE}</h1>
{LOOP_START:comment}
<strong>{username}</strong>
<p>{comment}</p>
<br />
{LOOP_END:comment}
</body>
</html>
Аз си мислех, че с preg_match_all ще стане, обаче не се оправих...
preg_match_all ("!\(LOOP_START\)(.?)\(\/LOOP_END\)!", $value, $output);
Т.е. искам примерно в темплейт файла да има:
<html>
<head>
<title>title</title>
</head>
<body>
<h1>{COMMENTS_PAGE}</h1>
{LOOP_START:comment}
<strong>{username}</strong>
<p>{comment}</p>
<br />
{LOOP_END:comment}
</body>
</html>
Аз си мислех, че с preg_match_all ще стане, обаче не се оправих...
preg_match_all ("!\(LOOP_START\)(.?)\(\/LOOP_END\)!", $value, $output);
Код:
<?php
class Template{
private $tags = array();
private $cacheLifeTime;
private $cacheMod;
private $cacheDir;
private $viewDir;
private $cacheKey;
public function Display($view_name){
$pathtofile = $this->cacheDir."/".md5($this->cacheKey)."/".$view_name.".php";
if(!file_exists($this->viewDir."/".$view_name.".tpl")){
die("Failed to compile template: ".$view_name." not exists");
}else{
if($this->cacheMod == true){
if($this->isCached($view_name)){
include_once($this->cacheDir."/".md5($this->cacheKey)."/".$view_name.".php");
}else{
if(!is_dir($this->cacheDir."/".md5($this->cacheKey))){
mkdir($this->cacheDir."/".md5($this->cacheKey), 0, true);
}
$output = file_get_contents($this->viewDir."/".$view_name.".tpl");
if(count($this->tags) > 0){
foreach($this->tags as $key => $value){
$output = str_replace('{'.$key.'}', $value, $output);
}
}
$fp = fopen($this->cacheDir."/".md5($this->cacheKey)."/".$view_name.".php", "w+");
fwrite($fp, $output);
fclose($fp);
include_once($this->cacheDir."/".md5($this->cacheKey)."/".$view_name.".php");
}
}else{
$output = file_get_contents($this->viewDir."/".$view_name.".tpl");
if(count($this->tags) > 0){
foreach($this->tags as $key => $value){
$output = str_replace('{'.$key.'}', $value, $output);
}
}
echo $output;
}
}
}
public function setVars($tag, $value){
if(is_array($tag) && is_array($value)){
foreach(array_combine($tag, $value) as $key => $value){
$this->tags[$key] = $value;
}
}elseif(!is_array($tag) && is_array($value) || is_array($tag) && !is_array($value)){
die("Failed to compile template: Tag or Value not array.");
}elseif(!is_array($tag) && !is_array($value)){
$this->tags[$tag] = $value;
}
}
public function setCacheMod($bool){
if(!is_bool($bool)){
$this->cacheMod = false;
}else{
$this->cacheMod = $bool;
}
}
public function setCacheLifeTime($seconds){
if(!is_numeric($seconds)){
$this->cacheLifeTime = 0;
}else{
$this->cacheLifeTime = round($seconds);
}
}
public function setCacheKey($key){
$this->cacheKey = $key;
}
public function setDirs($cache, $tmpl){
if($cache != ""){
$this->cacheDir = $cache;
}
if($tmpl != ""){
$this->viewDir = $tmpl;
}
}
private function isCached($view_name){
$pathtofile = $this->cacheDir."/".md5($this->cacheKey)."/".$view_name.".php";
if(file_exists($pathtofile)){
if(filemtime($pathtofile) > (time() - $this->cacheLifeTime)){
return true;
}else{
unlink($pathtofile);
return false;
}
}
}
}
?>