簡単なテンプレートクラス(PHP)
次に、データ操作クラスがあり、プロジェクトは簡単にデータを操作するしかありませんが、美工と一緒に素晴らしいページを表示できるようにするには、比較的良いテンプレートエンジンが必要です.SMARTYのような膨大なテンプレートエンジンに比べて、これはずっと小さいと思います.
このテンプレート類は私が以前ネットで見たもので、よく書けているので、引用しましたが、作者が誰なのか分かりません.ここではまずこの類の原理を話します.
まず,このクラスには単純な正則解析器が1つしかない.しかし、基本的には使えます.さらにその上で広がることができれば、私はこの小物が発展していると信じています.同じ趣味を持っている同志たちに、彼の目的を強化するために参加してください.私はこれからレンガを投げます.
template.class.php
このテンプレートがあれば、大きなテンプレートを忘れることができます.結局、あなたの美工は彼のスライスファイルにそんなに多くのSMARTYのマークを書くことはありません.彼が切ったら、images、css、jsアドレスを修正する必要はありません.
このテンプレート類は私が以前ネットで見たもので、よく書けているので、引用しましたが、作者が誰なのか分かりません.ここではまずこの類の原理を話します.
まず,このクラスには単純な正則解析器が1つしかない.しかし、基本的には使えます.さらにその上で広がることができれば、私はこの小物が発展していると信じています.同じ趣味を持っている同志たちに、彼の目的を強化するために参加してください.私はこれからレンガを投げます.
template.class.php
<?php
class template{
//
private $vars = array();
//
public $template_dir = './template/';
//
public $cache_dir = './cache/';
//
public $template_c_dir = './template_c/';
//
public $template_file = '';
//
public $left_delimiter = '<{';
//
public $right_delimiter = '}>';
//
private $template_c_file = '';
//
private $cache_file = '';
//
public $cache_time = 0;
//
private $preg_temp = array(
'~<\{(\$[a-z0-9_]+)\}>~i'
=> '<?php echo $1; ?>', // <{$name}>
'~<\{(\$[a-z0-9_]+)\.([a-z0-9_]+)\}>~i'
=> '<?php echo $1[\'$2\']; ?>', // <{$arr.key}>
'~<\{(\$[a-z0-9_]+)\.([a-z0-9_]+)\.([a-z0-9_]+)\}>~i'
=> '<?php echo $1[\'$2\'][\'$3\']; ?>', // <{$arr.key.key2}>
'~<\?php\s+(include_once|require_once|include|require)\s*\(\s*(.+?)\s*\)\s*;?\s*\?>~i'
=> '<?php include \$this->_include($2); ?>', // <?php include('inc/top.php'); ?>
'~<\{:(.+?)\}>~' => '<?php echo $1; ?>', // <{:strip_tags($a)}>
'~<\{\~(.+?)\}>~' => '<?php $1; ?>', // <{~var_dump($a)}>
'~<\?=\s*~' => '<?php echo ', // <?=
);
/**
*
*/
public function __construct(){
if(defined('TMP_PATH')){
$this->template_c_dir = TMP_PATH . 'template_c/';
$this->cache_dir = TMP_PATH . 'cache/';
}
}
/**
*
*@param $key mixed
*@param $value mixed
*/
public function assign($key , $value = ''){
if(is_array($key)){
$this->vars=array_merge($key,$this->vars);
}
else{
$this->vars[$key]=$value;
}
}
/**
*
*@param $file string
*/
public function display($file){
echo $this->fetch($file);
}
/**
*
*@param $file string
*@return $content string
*/
public function fetch($file){
$this->template_file = $file;
$desc_template_file = $this->template_dir .$file;
$desc_content = $this->readfile($desc_template_file);
$template_c_file_time= filemtime($desc_template_file);
// ,
if($this->cache_time < time()-$template_c_file_time){
$this->complie($this->token($desc_content));
}
//
ob_start();
@extract($this->vars , EXTR_OVERWRITE);
include ($this->template_c_dir . $this->template_c_file);
$content = ob_get_contents();
ob_end_clean();
//$this->store_buff($content);
return $content;
}
/*
* ,
*@param $content string
*@return $token_content string
*/
public function token($content){
$token_content = $content;
if($left_delimiter != '<{'){
$token_content = str_replace($left_delimiter , '<{' , $token_content);
$token_content = str_replace($right_delimiter, '}>' , $token_content);
}
$token_content = preg_replace(array_keys($this->preg_temp), $this->preg_temp , $token_content);
return $token_content;
}
/*
*
*@param $content string
*
public function store_buff($content){
$this->cache_file = md5($this->template_file) . $this->template_file . '.html';
$tempfile = $this->cache_dir . $this->cache_file;
$fp = fopen($tempfile , 'w');
fputs($fp,$content);
fclose($fp);
unset($fp);
}
*/
/*
*
*@param $content string
*
*/
public function complie($content){
$this->template_c_file = md5($this->template_file) . $this->template_file . '.php';
$tempfile = $this->template_c_dir . $this->template_c_file;
$fp = fopen($tempfile , 'w');
fputs($fp,$content);
fclose($fp);
unset($fp);
}
/*
*
*@param $file string
*@return $content string
*/
public function readfile($file){
if(file_exists($file)){
$fp = fopen($file , 'r');
$content ='';
while(!feof($fp)){
$content .= fgets($fp,4096);
}
fclose($fp);
unset($fp);
return $content;
}
else{
exit($file . ' not exist!');
}
}
/*
*
*@param $file string
*@return string
*/
public function _include($file){
if(file_exists($this->template_dir . $file)){
return ($this->template_dir . $file);
}
else{
echo " ";
exit;
}
}
}
?>
このテンプレートがあれば、大きなテンプレートを忘れることができます.結局、あなたの美工は彼のスライスファイルにそんなに多くのSMARTYのマークを書くことはありません.彼が切ったら、images、css、jsアドレスを修正する必要はありません.