模板继承

来源:互联网 发布:js判断单选框是否选中 编辑:程序博客网 时间:2024/05/01 17:29

T.php

<?php
define 
( 'ROOT' dirname  __FILE__  ) .  DIRECTORY_SEPARATOR  );
require_once
'Smarty.class.php' ;
class 
extends  Smarty  {
    const 
INSTANCE_NAME  = 'instanceName' ;
    public function 
__construct () {
        
// make sure E_STRICT is turned off
        //        $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  );
    }
    public function 
__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 ;
    }
    public function 
__get ( $var ) {
        
//        $er = error_reporting ( E_ALL ^ E_NOTICE );
        
$ret  $this -> smarty -> $var ;
        
error_reporting  $er  );
        return 
$ret ;
    }
    public function 
__set ( $var $value ) {
        
//        $er = error_reporting ( E_ALL ^ E_NOTICE );
        
$ret  = ( $this -> smarty -> $var  $value );
        
error_reporting  $er  );
        return 
$ret ;
    }
    public function 
display ( $resource_name ) {
        echo 
$this -> fetch  $resource_name  );
    }
    public function 
fetch ( $resource_name ) {
        
$ret  parent :: fetch  $resource_name  );
        while ( 
$resource  $this -> _derived  )
        {
            
$this -> _derived  null ;
            
$ret  parent :: fetch  $resource  );
        }
        return 
$ret ;
    }
    protected 
$smarty ;
    
// template inheritance
    
public  $_blocks  = array ();
    public 
$_derived  null ;
}
function 
smarty_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 ];
}
function 
smarty_extends ( $params , & $smarty ) {
    
$ss  $smarty -> get_template_vars  T :: INSTANCE_NAME  );
    
$ss -> _derived  $params  [ 'file' ];
}
?>

box.html

<?xml version = "1.0″ encoding=" UTF - 8″?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN" “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8″ />
<style type="text/css">
{
literal
}
body {
    margin: 0;
    padding: 0;
    font-family: Helvetica, Arial, sans-serif;
    font-size: 12px;
}
#wrap {
    margin: 50px auto;
    padding: 0;
    width: 500px;
    background: #dfd8c7;
    border: 1px solid #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: 2px 5px 3px 4px;
    font-size: 16px;
    border-bottom: 1px solid #cfc7b7;
}
fieldset {
    border: none;
    margin: 0;
    padding: 0;
}
label {
    width: 220px;
    text-align: right;
    display: block;
    float: left;
    clear: left;
    margin: 0;
    padding: 7px 5px 3px 5px;
}
input {
    float: left;
    border: 1px solid #cfc7b7;
    margin: 5px;
    padding: 3px;
}
input[type="submit"] {
    clear: left;
    margin: 5px 5px 5px 236px;
    padding: 2px 10px;
}
a {
    color: #000;
    text-decoration: none;
}
a:hover {
    text-decoration: underline;
}
{/
literal
}
</style>
<title>{block name="page_name"}{$page_name}{/block}</title>
</head>
<body>
<div id="wrap">
<div id="head">
<div id="version"><a href="http://spinlock.ch/projects/swisdk/">SWISDK v2.2</a></div>
</div>
<div id="body">
<h1>{block name="page_name"}{$page_name}{/block}</h1>
{block name="content"} {$content} {/block} <br style="line-height: 0; font-size: 1px" />
</div>

<div id="foot">
<div id="date">{$smarty.now|date_format:"%d. %B %Y"}</div>
</div>
</div>
{block name="messages"} {$messages} {/block}
</body>
</html>

404.html

{extends file="test/box.html"} 
{assign var="page_name" value="Site not found"} 
{block name="content"}
<p>You can try going <a href="javascript:back(-1)">one step back</a> in your history.</p>
{/block}
 

ps:

extends 标签的file属性可以是文件系统绝对路径, 或则是相对于smarty的template_dir属性所设置相对路径.

test.php

index.php

<?php
require  'T.php' ;

// 初始化模板类
$t  = new  T ();
//    var_dump($t);

$t -> display ( 'test/404.tpl' );
?>