【既然知道评价中肯,就应该明白差距在哪里了吧,每次运行都进行替换,相当程度上降低了系统性能,这样的模板引擎还有什么优势??】
【的确是有差距的,我看了一下,按照你说的一年多了,你已经累计有5次下载,和1毛钱的运营盈利。跟他这个只是会发个帖子,贴些代码,又没人下载,又没盈利的情况,差距实在太大了。】
<?php
class IndexAction extends YouYaX
{
public function index()
{
header("Content-type: text/html; charset=utf-8");
$this->assign("csdn","csdner,大家中午好")
->assign("head",array("来自","xxx"))
->assign("greet",array("<h2>PHP技术</h2>","基础编程","开放资源","framework"))
->assign("foot",array(
array("id"=>'1',"foot"=>"这是底部测试1"),
array("id"=>'2',"foot"=>"这是底部测试2"),
array("id"=>'3',"foot"=>"这是底部测试3")
))
->assign("bye","88,good bye");
$this->display("index.html");
}
}
?>
<html>
<head>
<title>welcome YouYaX</title>
</head>
<body>
<center>
<table width="400" >
<!-- 普通替换 -->
<tr>
<td>{csdn}</td>
</tr>
<!-- 一维数组替换 -->
<tr>
<td><loop> {head} ———— </loop> 发表在【{greet->1}】</td>
</tr>
</tr>
<!-- 一维数组替换 -->
<loop>
<tr><td>{greet}</td></tr>
</loop>
</table>
<hr>
<!-- 二维数组替换 -->
<list>
<h3>
{foot.foot}
</h3>
</list>
<!-- 普通替换 -->
{bye}
<hr>
比较测试 , 让我们在html中使用PHP吧 , 模板变量上要写上双引号 <br>
<?php
if( "{bye}"=="88,good bye" ){
echo "<font color=red>看吧 , 果然正确吧, 测试结束,测试者:{head->1}</font>";
}else{
echo "错了";
}
?>
</center>
</body>
</html>
【的确是有差距的,我看了一下,按照你说的一年多了,你已经累计有5次下载,和1毛钱的运营盈利。跟他这个只是会发个帖子,贴些代码,又没人下载,又没盈利的情况,差距实在太大了。】
<?php
class IndexAction extends YouYaX
{
public function index()
{
header("Content-type: text/html; charset=utf-8");
$this->assign("csdn","csdner,大家中午好")
->assign("head",array("来自","xxx"))
->assign("greet",array("<h2>PHP技术</h2>","基础编程","开放资源","framework"))
->assign("foot",array(
array("id"=>'1',"foot"=>"这是底部测试1"),
array("id"=>'2',"foot"=>"这是底部测试2"),
array("id"=>'3',"foot"=>"这是底部测试3")
))
->assign("bye","88,good bye");
$this->display("index.html");
}
}
?>
<html>
<head>
<title>welcome YouYaX</title>
</head>
<body>
<center>
<table width="400" >
<!-- 普通替换 -->
<tr>
<td>{csdn}</td>
</tr>
<!-- 一维数组替换 -->
<tr>
<td><loop> {head} ———— </loop> 发表在【{greet->1}】</td>
</tr>
</tr>
<!-- 一维数组替换 -->
<loop>
<tr><td>{greet}</td></tr>
</loop>
</table>
<hr>
<!-- 二维数组替换 -->
<list>
<h3>
{foot.foot}
</h3>
</list>
<!-- 普通替换 -->
{bye}
<hr>
比较测试 , 让我们在html中使用PHP吧 , 模板变量上要写上双引号 <br>
<?php
if( "{bye}"=="88,good bye" ){
echo "<font color=red>看吧 , 果然正确吧, 测试结束,测试者:{head->1}</font>";
}else{
echo "错了";
}
?>
</center>
</body>
</html>
//单个输出数组值
// var_dump($this->array_array);exit;
$return=preg_match_all('/\{[^{]*->[^{}]*}/',$txt,$single);
if($return){
foreach($single[0] as $v){
$x=strlen($v);
$y=$x-2;
$z=substr($v,1,$y); //title->0
$z1 = preg_split('/->/', $z, -1, PREG_SPLIT_NO_EMPTY);
$txt=str_replace($v,$this->array_array[$z1[0]][$z1[1]],$txt);
}
}
//数组替换
$return=preg_match_all("/<\s*loop\s*>\s*(.+?)\s*<\s*\/loop\s*>/s",$txt,$match);
if($return){
try{
foreach($match[1] as $lv){
if(eregi("<\s*loop\s*>",$lv)){
throw new Exception(htmlspecialchars("<loop>标签不能嵌套<loop>!"),"304");
break;
}
}
}catch(Exception $e){
$this->exception($e);
}
$result0=$match[0]; //<loop><tr><td>{array}</td></tr></loop> 系列数组 <loop>{array2}</loop>
$result1=$match[1]; //<tr><td>{array}</td></tr> 系列数组{array2}
//var_dump($result1);exit;
foreach($result1 as $r1){
preg_match_all("/\{(.*)\}/",$r1,$match2);
$result2=$match2[0][0];//{array}
$result3=$match2[1][0];//array
$result4=$this->array_array[$result3];//得到的数组
try{
if(!is_array($result4))
throw new Exception(htmlspecialchars("<loop>标签解析出错,仅支持一维数组!"),"306");
/*
if(empty($result4))
throw new Exception(htmlspecialchars("<loop>解析一维数组值为空!"),"308");
*/
}catch(Exception $e){
$this->exception($e);
}
$result5='';
// unset($result5); //result5 清零
foreach($result4 as $v){
$result5.=str_replace("{".$result3."}",$v,$r1);
}
foreach($result0 as $r0){
if(ereg($result2,$r0))
$txt=str_replace($r0,$result5,$txt);
}
}
}
//--数组替换
//include替换
$return=preg_match_all("/<\s*include\s+file=\"(.*)\"\s*>/",$txt,$inc);
if($return){
$inc1=$inc[0];//<include file="top.html">系列数组
$inc2=$inc[1];//top.html 系列数组
foreach($inc2 as $v){
if(file_exists($v)){
$sub=file_get_contents($v);
foreach($inc1 as $v1){
//区分大小写匹配
if(ereg($v,$v1)){
$txt=str_replace($v1,$sub,$txt);
}
}
}else{
exit("include标签解析出错!");
}
}
}在嵌套的循环中的一系列的
$txt=str_replace($v,$this->array_array[$z1[0]][$z1[1]],$txt);
result5.=str_replace("{".$result3."}",$v,$r1);
$txt=str_replace($r0,$result5,$txt);
txt=str_replace($v1,$sub,$txt);对每一个提供的数据都要执行一次 str_replace
读取后,整个变量$content 用eval后就直接变成了PHP执行了
纯属意淫哈。
内部的不用看了,开发者不需要看的,大不了就是效率的问题吧,而且这个效率的问题也不好测试,又不直观,只要外观清晰,代码易写,逻辑清晰 即可,其他的模板效率也不一定高,而且替换str_replace等也不一定就效率一定差。
自己想想,孰是孰非
<html>
<head>
<title>welcome YouYaX</title>
</head>
<body>
<center>
<table width="400" >
<!-- 普通替换 -->
<tr>
<td><?php echo $csdn</td>
</tr>
<!-- 一维数组替换 -->
<tr>
<td><?php foreach($head as $v){ echo $v;}?> ———— <?php }?> 发表在【<?php echo $greet[1] ?>】</td>
</tr>
</tr>
<!-- 一维数组替换 -->
<?php foreach($greet as $v) { ?>
<tr><td><?php echo $v ?></td></tr>
<?php } ?>
</table>
<hr>
<!-- 二维数组替换 -->
<?php foreach($foot as $v) { ?>
<h3>
<?php echo $v['foot'] ?>
</h3>
<?php } ?>
<!-- 普通替换 -->
<?php echo $bye ?>
<hr>
比较测试 , 让我们在html中使用PHP吧 , 模板变量上要写上双引号 <br>
<?php
if( "{bye}"=="88,good bye" ){
echo "<font color=red>看吧 , 果然正确吧, 测试结束,测试者:{head->1}</font>";
}else{
echo "错了";
}
?>
</center>
</body>
</html>
而且你的改编代码正好验证、对应了我的模板分离思想。本应该像xuzuning所写那样的代码,如果要使php,html分开来,或者给不懂php代码的美工使用,我就做到了,,嘎嘎~~
主要是给他们瞅瞅什么叫专业的模板引擎,以前只是放出一个pdf文件,显得很不直观,这次写一些简单的代码。对每一个模板标记在“后台”进行严格的测试和匹配使前端保持清晰的结构,简单易用的数据替换我估计学php的人看这些代码 1 分钟,大概就能很不费力地清楚这个模板或框架的使用方法了
要么自己echo直接输出Html, 自己的世界自己说的算, 想用模板就引入smarty.