当前位置: 首页 > 现代文学 > 文章

UChome PHP模板引擎

2019-08-13

	UChome PHP模板引擎

最近接手一个WEB产品,用PHP写的,发现里面使用了模板引擎从UChome这类产品中抽取出来的。

这个模板引擎的特点非常让我喜欢,简洁不冗余,原理简单明了。

在以后的项目中真的可以考虑使用此类小巧而精致的模板引擎。

1).调用模板(记得先引入2的文件)include_oncetemplate("test");//模板的名字test2).模板调用逻辑template(){,,([mobile]){=S_ROOT../api/mobile/tpl_...php;(!()){showmessage(m_function_is_disable_on_wap);}}{(strexists(,/)){=}{="template/$_SCONFIG[template]/$name";}=S_ROOT../data/tpl_cache/.(/,_,)..php;(1===[debug]){(S_ROOT../core/function_);parse_template();}{(!()){(S_ROOT../core/function_);parse_template();}}}}subtplcheck(,,){,([tplrefresh]([tplrefresh]==1||(1,[tplrefresh])==1)){=(|,);(){=S_ROOT../...htm;(!()){=(/.[template]./,/default/,);}=();(){(S_ROOT../source/function_);parse_template();}}}}3).模板解析函数(!(IN_UChome)){(AccessDenied);}[i]=0;[block_search]=[block_replace]=();parse_template(){,,=([yoho_.._template]):[yoho_.._template];[sub_tpls]=();=S_ROOT../...htm;=S_ROOT../data/tpl_cache/.(/,_,)..php;(!()){=(/.[template]./,/default/,);}=sreadfile();(()){("Templatefile:$tplfileNotfoundorhavenoaccess!");}=("/\\!\-\-\{template\s+([a-z0-9_\/]+)\}\-\-\/ie","readtemplate(\\1)",);=("/\\!\-\-\{template\s+([a-z0-9_\/]+)\}\-\-\/ie","readtemplate(\\1)",);=("/\\!\-\-\{block\/(.+)\}\-\-\/ie","blocktags(\\1)",);=("/\\!\-\-\{ad\/(.+)\}\-\-\/ie","adtags(\\1)",);=("/\\!\-\-\{date\((.+)\)\}\-\-\/ie","datetags(\\1)",);=("/\\!\-\-\{avatar\((.+)\)\}\-\-\/ie","avatartags(\\1)",);=("/\\!\-\-\{eval\s+(.+)\s*\}\-\-\/ies","evaltags(\\1)",);="((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\"\\[\]\$\x7f-\xff]+\])*)";$template=preg_replace("/\\!\-\-\{(.+)\}\-\-\/s","{\\1}",$template);$template=preg_replace("/([\r]+)\t+/s","\\1",$template);$template=preg_replace("/(\\\$[a-zA-Z0-9_\[\]\\"\$\x7f-\xff]+)\.([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/s","\\1[\\2]",);=("/\{(\\\$[a-zA-Z0-9_\[\]\\"\$\.\x7f-\xff]+)\}/s","=\\1",$template);$template=preg_replace("//es","addquote(=\\1)",$template);$template=preg_replace("/\\\=\\\=\\\\/es","addquote(=\\1)",$template);//逻辑$template=preg_replace("/\{\s+(.+)\}/ies","stripvtags(php}elseif(\\1){,)",$template);$template=preg_replace("/\{\}/is","php}{",$template);//循环for($i=0;$i6;$i++){$template=preg_replace("/\{loop\s+(\S+)\s+(\S+)\}(.+)\{\/loop\}/ies","stripvtags(phpif(is_array(\\1)){foreach(\\1as\\2){,\\3php}})",$template);$template=preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}(.+)\{\/loop\}/ies","stripvtags(phpif(is_array(\\1)){foreach(\\1as\\2=\\3){,\\4php}})",$template);$template=preg_replace("/\{\s+(.+)\}(.+)\{\/\}/ies","stripvtags(phpif(\\1){,\\2php})",$template);}//常量$template=preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/s","=\\1",$template);//替换if(!empty($_SGLOBAL[block_search])){$template=str_replace($_SGLOBAL[block_search],$_SGLOBAL[block_replace],$template);}//换行$template=preg_replace("/\\[\r]*\\/s","",$template);//附加处理$template=$template."php="phpif(!defined(IN_KUAIPAN))exit(AccessDenied);phpsubtplcheck(".(|,[sub_tpls]).",$_SGLOBAL[timestamp],$tpl);$templatephpob_out();(!swritefile(,)){("File:$objfilecannotbewrite!");}}addquote(){("\\\"","\"",("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s","[\\1]",));}striptagquotes(){=("/\\\=(\\\$.+)\\/s","\\1",);=("\\\"","\"",("/\[\([a-zA-Z0-9_\-\.\x7f-\xff]+)\\]/s","[\\1]",));}evaltags(){[i]++;="!--EVAL_TAG_{$_SGLOBAL[i]}--[block_search][[i]]=[block_replace][[i]]="php".stripvtags()."}blocktags(){[i]++;="!--BLOCK_TAG_{$_SGLOBAL[i]}--[block_search][[i]]=[block_replace][[i]]="phpblock(\"\");}adtags(){[i]++;="!--AD_TAG_{$_SGLOBAL[i]}--[block_search][[i]]=[block_replace][[i]]="phpadshow($pagetype);}datetags(){[i]++;="!--DATE_TAG_{$_SGLOBAL[i]}--[block_search][[i]]=[block_replace][[i]]="phpechosgmdate($parameter);}avatartags(){[i]++;="!--AVATAR_TAG_{$_SGLOBAL[i]}--[block_search][[i]]=[block_replace][[i]]="phpechoavatar($parameter);}stripvtags(,=){=("\\\"","\"",("/\\\=(\\\$.+)\\/s","\\1",));=("\\\"","\"",);.;}readtemplate(){,=strexists(,/):"template/$_SCONFIG[template]/$name";=S_ROOT../...htm;[sub_tpls][]=(!()){=(/.[template]./,/default/,);}=sreadfile();}postedon2011-11-2823:20阅读(3288)所属分类:。