T.php
<?php
define('ROOT',dirname(__FILE__).DIRECTORY_SEPARATOR);
require_once'Smarty.class.php';
classTextendsSmarty{
constINSTANCE_NAME='instanceName';
publicfunction__construct(){
//makesureE_STRICTisturnedoff
//$this->error_reporting=E_ALL^E_NOTICE;
$this->compile_dir=ROOT.'templates_c';
$this->template_dir=ROOT.'templates';
$this->cache_dir=ROOT.'cache';
$this->config_dir=ROOT.'config';
$this->caching=false;
$this->security=false;
$this->register_block('block','smarty_block');
$this->register_function('extends','smarty_extends');
$this->assign_by_ref(self::INSTANCE_NAME,$this);
}
publicfunction__call($method,$args){
//$er=error_reporting(E_ALL^E_NOTICE);
$ret=call_user_func_array(array(
&$this->smarty,$method
),$args);
error_reporting($er);
return$ret;
}
publicfunction__get($var){
//$er=error_reporting(E_ALL^E_NOTICE);
$ret=$this->smarty->$var;
error_reporting($er);
return$ret;
}
publicfunction__set($var,$value){
//$er=error_reporting(E_ALL^E_NOTICE);
$ret=($this->smarty->$var=$value);
error_reporting($er);
return$ret;
}
publicfunctiondisplay($resource_name){
echo$this->fetch($resource_name);
}
publicfunctionfetch($resource_name){
$ret=parent::fetch($resource_name);
while($resource=$this->_derived)
{
$this->_derived=null;
$ret=parent::fetch($resource);
}
return$ret;
}
protected$smarty;
//templateinheritance
public$_blocks=array();
public$_derived=null;
}
functionsmarty_block($params,$content,&$smarty,&$repeat){
if($content===null)
return;
$name=$params['name'];
$ss=$smarty->get_template_vars(T::INSTANCE_NAME);
if(!isset($ss->_blocks[$name]))
$ss->_blocks[$name]=$content;
return$ss->_blocks[$name];
}
functionsmarty_extends($params,&$smarty){
$ss=$smarty->get_template_vars(T::INSTANCE_NAME);
$ss->_derived=$params['file'];
}
?>
box.html
<?xmlversion="1.0″encoding="UTF-8″?>
<!DOCTYPEhtmlPUBLIC“-//W3C//DTDXHTML1.0Strict//EN"“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8″/>
<styletype="text/css">
{
literal
}
body{
margin:0;
padding:0;
font-family:Helvetica,Arial,sans-serif;
font-size:12px;
}
#wrap{
margin:50pxauto;
padding:0;
width:500px;
background:#dfd8c7;
border:1pxsolid#cfc7b7;
}
#head,#foot{
position:relative;
height:11px;
}
#head#version{
position:absolute;
top:0px;
right:3px;
font-size:9px;
}
#foot#date{
position:absolute;
bottom:1px;
left:3px;
font-size:9px;
}
#body{
background:#efe8d7;
margin:1px;
font-size:12px;
}
h1{
margin:0;
padding:2px5px3px4px;
font-size:16px;
border-bottom:1pxsolid#cfc7b7;
}
fieldset{
border:none;
margin:0;
padding:0;
}
label{
width:220px;
text-align:right;
display:block;
float:left;
clear:left;
margin:0;
padding:7px5px3px5px;
}
input{
float:left;
border:1pxsolid#cfc7b7;
margin:5px;
padding:3px;
}
input[type="submit"]{
clear:left;
margin:5px5px5px236px;
padding:2px10px;
}
a{
color:#000;
text-decoration:none;
}
a:hover{
text-decoration:underline;
}
{/
literal
}
</style>
<title>{blockname="page_name"}{$page_name}{/block}</title>
</head>
<body>
<divid="wrap">
<divid="head">
<divid="version"><ahref="http://spinlock.ch/projects/swisdk/">SWISDKv2.2</a></div>
</div>
<divid="body">
<h1>{blockname="page_name"}{$page_name}{/block}</h1>
{blockname="content"}{$content}{/block}<brstyle="line-height:0;font-size:1px"/>
</div>
<divid="foot">
<divid="date">{$smarty.now|date_format:"%d.%B%Y"}</div>
</div>
</div>
{blockname="messages"}{$messages}{/block}
</body>
</html>
404.html
{extendsfile="test/box.html"}
{assignvar="page_name"value="Sitenotfound"}
{blockname="content"}
<p>Youcantrygoing<ahref="javascript:back(-1)">onestepback</a>inyourhistory.</p>
{/block}
ps:
extends 标签的file属性可以是文件系统绝对路径, 或则是相对于smarty的template_dir属性所设置相对路径.
test.php
index.php
<?php
require'T.php';
//初始化模板类
$t=newT();
//var_dump($t);
$t->display('test/404.tpl');
?>