我的朋友我就簡單的PHP模板引擎的工作這裏是一些代碼可以someboady幫我補充文件,包括功能 喜歡%include.sidebar.html%和循環功能,這樣我就可以運行循環或告訴我一些其他簡單的PHP模板引擎簡單的PHP模板引擎
<?
class Template {
private $template, $vars;
public function SetTemplete($tempname) {
$templatePathAndName = $tempname;
if(file_exists($templatePathAndName))
$this->template = file_get_contents($templatePathAndName);
else
die("Template not found... aborting...");
}
public function setVar($var, $content) {
$this->vars[$var] = $content;
}
public function replaceAll() {
foreach($this->vars as $var => $content) {
$this->template = str_replace("{" . strtoupper($var). "}", $content, $this->template);
}
}
public function publish() {
$this->replaceAll();
echo $this->template;
}
public function includeFile(){
foreach($this->vars as $var => $content) {
$this->template = str_replace("<-" . strtoupper($var). "->",
file_get_contents($content),
$this->template);
}
}
$tpl = new Template;
$tpl->SetTemplete(base_path."home.html");
$tpl->setVar("SITE_NAME", "Simple Template Class2");
$tpl->publish();
?>
///home.html
<html>
<head>
</head>
<body>
{SITE_NAME}
</body>
</html>
有趣的閱讀:http://fabien.potencier.org/article/34/templating-engines-in-php和http://fabien.potencier.org/article/35/templating-engines-in-php-follow-向上 – igorw 2010-07-04 09:56:46