模板库
<?phpif (! defined ( 'BASEPATH' ))exit ( 'No direct script access allowed' );
function addquote($var) {
return str_replace ( "\\\"", "\"", preg_replace ( "/\[(+)\]/s", "['\\1']", $var ) );
}
function stripvtags($expr, $statement) {
$expr = str_replace ( "\\\"", "\"", preg_replace ( "/\<\?\=(\\\$.+?)\?\>/s", "\\1", $expr ) );
$statement = str_replace ( "\\\"", "\"", $statement );
return $expr . $statement;
}
class CI_Template {
var $TemplateRootPath = "./templates/";
var $TemplatePath = "";
var $TemplateDir = "default";
var $CompiledFolder = "views";
var $CompiledPath = "";
var $TemplateFile = "";
var $CompiledFile = "";
var $TemplateString = "";
var $TemplateExtension = '.html';
var $CompiledExtension = '.php';
var $LinkFileType = 'css|js|jpeg|jpg|png|bmp|gif|swf';
/**
* Constructor
*/
public function __construct() {
$this->TemplateRootPath = "./application/template/";
$this->TemplateDir = 'views';
$this->TemplatePath = $this->TemplateRootPath . $this->TemplateDir . '/';
$this->CompiledPath = './application/' . $this->TemplateDir . '/';
}
function Template($filename) {
$this->TemplateFile = $this->TemplatePath . $filename . $this->TemplateExtension;
$this->CompiledFile = $this->CompiledPath . $filename . $this->CompiledExtension;
if (! is_file ( $this->TemplateFile )) {
$tpl_path = strpos ( $this->TemplateDir, '/' ) ? dirname ( $this->TemplatePath ) . '/' : dirname ( $this->TemplatePath ) . '/default/';
$this->TemplateFile = $tpl_path . $filename . $this->TemplateExtension;
if (! is_file ( $this->TemplateFile )) {
die ( "模板文件'" . $this->TemplateFile . "'不存在,请检查目录" );
}
}
if ($this->Load ()) {
$this->Compile ();
$this->Write ();
} else {
return false;
}
return $this->CompiledFile;
}
function EvalTemplate($filename) {
$this->TemplateFile = $this->TemplatePath . $filename . $this->TemplateExtension;
$this->Load ();
$contents = str_replace ( '"', '\"', $this->TemplateString );
return "return \"{$contents}\";";
}
function Load() {
$fp = fopen ( $this->TemplateFile, 'rb' );
if ($fp) {
if (! $this->TemplateString = @fread ( $fp, filesize ( $this->TemplateFile ) )){
die ( "模板文件'" . $this->TemplateFile . "'不存在和内容错误,请检查文件" );
}
}
fclose ( $fp );
return true;
}
function Compile() {
global $rewriteHandler;
$var_regexp = "((\\\$*)(-\>*)?(\[\$\x7f-\xff]+\])*)";
$const_regexp = "(*)";
$nest = 5;
$template = $this->TemplateString;
$template = preg_replace ( "/(\<form.*? method=[\"\']?post[\"\']?)([^\>]*\>)/i", "\\1 \\2\n<input type=\"hidden\" name=\"FORMHASH\" value='{FORMHASH}'/>", $template );
$template = preg_replace ( "/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $template );
$template = str_replace ( "{LF}", "<?=\"\\n\"?>", $template );
$template = preg_replace ( "/\{$var_regexp\}/s", "<?=\\1?>", $template );
$template = preg_replace ( "/$var_regexp/es", "addquote('<?=\\1?>')", $template );
$template = preg_replace ( "/\<\?\=\<\?\=$var_regexp\?\>\?\>/es", "addquote('<?=\\1?>')", $template );
$template = preg_replace ( "/[\n\r\t]*\{template\s+(+)\}[\n\r\t]*/is", "<? include \$this->TemplateHandler->template('\\1'); ?>", $template );
$template = preg_replace ( "/[\n\r\t]*\{template\s+(.+?)\}[\n\r\t]*/is", "<? include \$this->TemplateHandler->template(\\1); ?>", $template );
$template = preg_replace ( "/[\n\r\t]*\{eval\s+(.+?)\}[\n\r\t]*/ies", "stripvtags('<? \\1 ?>','')", $template );
$template = preg_replace ( "/[\n\r\t]*\{conf\s+(.+?)\}[\n\r\t]*/ies", "addquote('<?=\$this->Config[\\1]?>')", $template );
$template = preg_replace ( "/[\n\r\t]*\{echo\s+(.+?)\}[\n\r\t]*/ies", "stripvtags('<? echo \\1; ?>','')", $template );
$template = preg_replace ( "/[\n\r\t]*\{elseif\s+(.+?)\}[\n\r\t]*/ies", "stripvtags('<? } elseif(\\1) { ?>','')", $template );
$template = preg_replace ( "/[\n\r\t]*\{else\}[\n\r\t]*/is", "<? } else { ?>", $template );
$template = preg_replace ( "/[\n\r\t]*\{BASE_URL\}[\n\r\t]*/is", base_url (), $template );
for($i = 0; $i < $nest; $i ++) {
$template = preg_replace ( "/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\}[\n\r]*(.+?)[\n\r]*\{\/loop\}[\n\r\t]*/ies", "stripvtags('<? if(is_array(\\1)) { foreach(\\1 as \\2) { ?>','\\3<? } } ?>')", $template );
$template = preg_replace ( "/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}[\n\r\t]*(.+?)[\n\r\t]*\{\/loop\}[\n\r\t]*/ies", "stripvtags('<? if(is_array(\\1)) { foreach(\\1 as \\2 => \\3) { ?>','\\4<? } } ?>')", $template );
$template = preg_replace ( "/[\n\r\t]*\{if\s+(.+?)\}[\n\r]*(.+?)[\n\r]*\{\/if\}[\n\r\t]*/ies", "stripvtags('<? if(\\1) { ?>','\\2<? } ?>')", $template );
$template = preg_replace ( "/[\n\r\t]*\{while\s+(.+?)\}[\n\r]*(.+?)[\n\r]*\{\/while\}[\n\r\t]*/ies", "stripvtags('<? while(\\1) { ?>','\\2<? } ?>')", $template );
}
$template = preg_replace ( "/\{$const_regexp\}/s", "<?=\\1?>", $template );
$template = trim ( $template );
$this->TemplateString = $template;
if (! empty ( $this->LinkFileType )) {
$this->ModifyLinks ();
}
if ($rewriteHandler) {
$this->TemplateString = $rewriteHandler->output ( $this->TemplateString, true );
}
}
function write() {
$save_dir = dirname ( $this->CompiledFile );
if (! is_dir ( $save_dir ))
$this->MakeDir ( $save_dir, 0777 );
$fp = fopen ( $this->CompiledFile, 'wb' );
if (! $fp)
die ( '模板无法写入,请检查目录是否有可写' );
$length = fwrite ( $fp, $this->TemplateString );
fclose ( $fp );
return $length;
}
function MakeDir($dir_name, $mode = 0777) {
$dir_name = str_replace ( "\\", "/", $dir_name );
$dir_name = preg_replace ( "#(/" . "/+)#", "/", $dir_name );
if (is_dir ( $dir_name ) !== false)
Return true;
$dir_name = explode ( "/", $dir_name );
$dirs = '';
foreach ( $dir_name as $dir ) {
if (trim ( $dir ) != '') {
$dirs .= $dir . "/";
if (is_dir ( $dirs ) == false && @mkdir ( $dirs, $mode ) === false) {
return false;
} else {
;
}
}
}
return true;
}
function ModifyLinksbak() {
preg_match_all ( "/src=[\"\'\s]?(.*?)[\"\'\s]|url[\(\"\']{1,3}(.*?)[\s\"\'\)]|background=[\"\']?(.*?)[\"\'\s]|href=[\"\'\s]?(.*?)[\"\'](.*?)\>/si", $this->TemplateString, $match );
$old = @array_values ( array_merge ( @array_unique ( $match ), $match , @array_unique ( $match ), $match ) );
$old = array_unique ( $old );
$old = preg_grep ( "~.*?\.(" . $this->LinkFileType . ")$~i", $old );
foreach ( $old as $link ) {
if (trim ( $link ) != "" and ! strpos ( $link, ':/' . '/' )) {
if (strpos ( $link, '../' ) === 0) {
$this->TemplateString = str_replace ( $link, dirname ( $this->TemplatePath ) . '/' . ltrim ( $link, './' ), $this->TemplateString );
} else {
$this->TemplateString = str_replace ( $link, rtrim ( $this->TemplatePath, '\/' ) . '/' . ltrim ( $link, './' ), $this->TemplateString );
}
}
}
return $this->TemplateString;
}
function ModifyLinks() {
preg_match_all ( "/src=[\"\'\s]?(.*?)[\"\'\s]|url[\(\"\']{1,3}(.*?)[\s\"\'\)]|background=[\"\']?(.*?)[\"\'\s]|href=[\"\'\s]?(.*?)[\"\'](.*?)\>/si", $this->TemplateString, $match );
$old = @array_values ( array_merge ( @array_unique ( $match ), $match , @array_unique ( $match ), $match ) );
$old = array_unique ( $old );
$old = preg_grep ( "~.*?\.(" . $this->LinkFileType . ")$~i", $old );
foreach ( $old as $link ) {
if (trim ( $link ) != "" and false === strpos ( $link, ':/' . '/' )) {
$private_file = str_replace ( 'templates/default/', 'templates/' . $this->TemplateDir . '/', $link );
if (! is_file ( $private_file ) && false === strpos ( $private_file, 'templates' )) {
$private_file = 'templates/' . $this->TemplateDir . '/' . $private_file;
}
if ('default' != $this->TemplateDir && ! is_file ( $private_file )) {
$private_file = str_replace ( 'templates/' . $this->TemplateDir . '/', 'templates/default/', $private_file );
}
if (is_file ( $private_file ) == false) {
continue;
}
$this->TemplateString = str_replace ( $link, $private_file, $this->TemplateString );
}
}
return $this->TemplateString;
}
function RepairBracket($var) {
return preg_replace ( "~\[(*?+*?)\]~i", "[\"\\1\"]", $var );
}
}
模板文件放在./application/template/default/目录里 以.html结尾的html文件 编译后的文件在./application/views/目录里以.PHP结尾文件存放.
用法:
把文件放在libraries目录 然后 在你的控制器里$this->load->library ( 'template' ); $this->template->Template ( 'header' ) ,header是存放在./application/template/default目录下的一个html文件,此库来自第三方,并非本人所著,只是用于学习, 标签用法和uchome差不多,也不完全.
例举几个吧: {BASE_URL}images/favicon.ico将被替换成 <?= base_url ();?>images/favicon.ico的结果 如:http://codeigniter.org.cn/images/favicon.ico{echo $hello}将被替换成<?php echo $hello; ?>
放假了, 没人。自己来个沙发 研究研究 还很有意思css|js|jpeg|jpg|png|bmp|gif|swf 不懂~~ 你这个东西有什么用? 我也没有看懂这个作什么用. blueflagbj 发表于 2011-10-6 16:44 static/image/common/back.gif
不懂~~
看一下说明就可 baiyuxiong 发表于 2011-10-6 23:43 static/image/common/back.gif
你这个东西有什么用?
Smarty总该知道吧 Erric_Zhu 发表于 2011-10-7 23:11 static/image/common/back.gif
我也没有看懂这个作什么用.
Smarty 的精简版 或许东西不错,不过你这个文章写的没头没尾,没看懂。
页:
[1]
2