zoukankan      html  css  js  c++  java
  • 模板引擎逻辑语句处理之对单层for循环的处理

    先上代码

    private function moldforExt(){//模板单层For循环处理
    		$SQL=$this->sql_obj;
    		$URL=$this->url_obj;
    		require($this->dataFile);
    		$con=$this->moldFileContent;
    		$con=str_replace("\n","<#tmoldbr#>",$con);
    		$preg_for_str="/<!\-\-\s*for\[([a-zA-Z_\-0-9]+):([a-zA-Z_\-0-9]+)\]\s*\-\->([^!]*)<!\-\-\s*endfor\s*\-\->/";
    		preg_match_all($preg_for_str,$con,$preg_for_match);
    		$preg_for_num=count($preg_for_match[0]);
    		for($for_tem_i=0;$for_tem_i<$preg_for_num;$for_tem_i++){
    			$tem_for_st=$preg_for_match[1][$for_tem_i];
    			$tem_for_ed=$preg_for_match[2][$for_tem_i];
    			if(isset($$tem_for_st)&&isset($$tem_for_ed)){
    				$for_tem_start=$$tem_for_st;
    				$for_tem_end=$$tem_for_ed;		
    				$for_tem_con="";
    				for($for_tem_j=$for_tem_start;$for_tem_j<$for_tem_end;$for_tem_j++){
    					$tttem_for_com=$preg_for_match[3][$for_tem_i];
    					$tttem_for_com=str_replace("({".$tem_for_st."})",$for_tem_j,$tttem_for_com);
    					$for_tem_con.=$tttem_for_com;
    				}
    				$con=str_replace($preg_for_match[0][$for_tem_i],$for_tem_con,$con);
    			}
    		}
    		$this->moldFileContent=str_replace("<#tmoldbr#>","\n",$con);
    	}
    

    其实里面用到的还是基于正则表达式

    在模板中所写的for循环样式如下

    <!-- for[strat:end] -->
    ...
    HTML代码
    ...
    <!-- endfor -->
    

    将它写成html注释的形式是为了防止在只浏览模板文件时造成排版错误。

    for循环的开始标识start会在模板数据文件中对$start的存在进行检测,这里面用到了PHP内部的可变变量,讲一个变量的值作为一个变量名来使用。

  • 相关阅读:
    求素数(定义法,埃式法,欧拉法)
    打表法
    python学习日记(匿名函数)
    python学习日记(编码再回顾)
    python学习日记(文件操作练习题)
    python学习日记(迭代器、生成器)-乱七八糟
    python学习日记(生成器函数进阶)
    python学习日记(装饰器的补充)
    python学习日记(函数--装饰器)
    python学习日记(函数进阶)
  • 原文地址:https://www.cnblogs.com/echosoar/p/4591865.html
Copyright © 2011-2022 走看看