模板继承
来源:互联网 发布:js判断单选框是否选中 编辑:程序博客网 时间:2024/05/01 17:29
T.php
<?php
define ( 'ROOT' , dirname ( __FILE__ ) . DIRECTORY_SEPARATOR );
require_once 'Smarty.class.php' ;
class T 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' );
?>
- 模板继承
- 模板继承
- 模板继承
- 模板继承
- 虚拟继承和模板继承
- 区分继承和模板
- 区分继承和模板
- 材质继承 模板材质
- 模板类的继承
- c++的模板继承
- 模板-继承-具现化
- 区分继承和模板
- C++模板和继承
- C++模板和继承
- ThinkPHP示例:模板继承
- 类模板的继承
- 模板与继承
- 模板类继承问题
- C++ 提权函数
- Nokia 5530XM
- Windows系统使用minGW+msys 编译ffmpeg 0.5的全过程详述
- Java中的private、public、protected
- 网络权限设置
- 模板继承
- 调试ATL工程设置断点无效的解决方法
- 乔致庸毕生的三大项目之三:汇通天下、货通天下
- 我的工作之旅
- 问一个C语言问题
- 学生成绩管理系统-C
- PHP设计模式实例之(观察者模式、策略模式、简单工厂模式)
- 在做与不做之间徘徊
- 关于随机的整理