Вот такой простенький шаблонизатор юзаю я
yuactpl.php
PHP код:
<?
if(!class_exists("yuactpl"))
{
class yuactpl
{
var $tpls=array();
var $ptpls=array(); //parsed templates
var $wtpls=array(); //while templates (blocks, used many times)
var $pwtpls=array(); //parsed while templates
//**
function file2string($file)
{
if (file_get_contents($file))
{
return file_get_contents($file);
}
else
{
return false;
}
/*
if(file_exists($file) && is_readable($file))
{
$f=fopen($file,"rb");
$filecontent=fread($f,filesize($file));
fclose($f);
return $filecontent;
}else
{
return false;
}
*/
}
//**
function loadtpl($tpl)
{
if($result=$this->file2string($tpl))
{
$this->tpls[$tpl]=preg_replace_callback('/\<script language="php"\>(.*)\<\/script\>/isU',create_function('$data','return eval($data[1]);'),$result);
return true;
}
return false;
}
//**
function parse($rarray,$tpl) //replace array ( array('title' => 'Welcome', 'body' => 'This is an example',) )
{
if(!isset($this->tpls[$tpl]) or !is_array($rarray)) return false;
//**
$tpltxt/*template text*/=$this->tpls[$tpl];
//**
foreach($rarray as $key=>$value)
{
if(!is_array($value)) $tpltxt=str_replace('{'.$key.'}', $value, $tpltxt);
}
//**
$this->ptpls[$tpl]=$tpltxt;
return true;
}
//**
function getparsedtext($tpl)
{
if(!isset($this->ptpls[$tpl])) return false;
//**
return $this->ptpls[$tpl];
}
//**
function loadwtpl($tpl,$delimiter="<!--delimiter-->")
{
if($result=$this->file2string($tpl))
{
$this->wtpls[$tpl]=explode($delimiter, preg_replace_callback('/\<php\>(.*)\<\/php\>/isU',create_function('$data','return eval($data[1]);'),$result));
return true;
}
return false;
}
//**
function wparse($rarray,$tpl,$n) //replace array ( array('title' => 'Welcome', 'body' => 'This is an example',) )
{
if(!isset($this->wtpls[$tpl][$n]) or !is_array($rarray)) return false;
//**
$tpltxt/*template text*/=$this->wtpls[$tpl][$n];
//**
foreach($rarray as $key=>$value)
{
if(!is_array($value)) $tpltxt=str_replace('{'.$key.'}', $value, $tpltxt);
}
//**
$this->pwtpls[$tpl][$n]=$tpltxt;
return true;
}
//**
function wparseall($rarray,$tpl)
{
if(!isset($this->wtpls[$tpl]) or !is_array($rarray)) return false;
//**
foreach($rarray as $key=>$value)
{
$this->wtpls[$tpl]=str_replace('{'.$key.'}', $value, $this->wtpls[$tpl]);
}
//**
return true;
}
//**
function getwparsedtext($tpl,$n)
{
if(!isset($this->pwtpls[$tpl][$n])) return false;
//**
return $this->pwtpls[$tpl][$n];
}
//**
function fastparse($tpl,$rarray=array(),$echo=true)
{
if(!isset($this->tpls[$tpl])) $this->loadtpl($tpl);
$this->parse($rarray,$tpl);
if($echo) echo $this->getparsedtext($tpl);
else return $this->getparsedtext($tpl);
}
//**
function fastwparse($tpl,$rarray=array(),$num=0,$echo=true)
{
if(!isset($this->wtpls[$tpl])) $this->loadwtpl($tpl);
$this->wparse($rarray,$tpl,$num);
if($echo) echo $this->getwparsedtext($tpl,$num);
else return $this->getwparsedtext($tpl,$num);
}
}
}
?>
index.php
PHP код:
<?php
include('yuactpl.php');
$title = "blabla" // допустим выбирается с БД
$content = "привет. я задрот...." // тоже с БД
$tp = new yuactpl;
$template = "./index.tpl";
$page_data = array(
'title' => $title,
'content' => $content
);
$tp->fastwparse($template, $page_data);
?>
index.tpl
Код HTML:
<html>
<head><title>{title}</title>
<meta http-equiv="content-type" content="text/html; charset=utf8"/>
</head>
<body>
{content}
</body>
</html>
Быстрый и удобный :)
PS
Ещё есть возможность использовать в шаблоне php код:
в тегах пишется
Код HTML:
<script language="php">echo "bla"</script>
Если интересно, почитайте http://forum.dklab.ru/viewtopic.php?p=38883
|