二开教程 Development

提供PHPCMS常用模块的二次开发,改造,升级都修改过程代码

phpcms 的模板模板机制

 admin  2023-07-03 16:54:33

phpcms有自己的一套模板机制,代码如下:

  1. function template_parse($str$istag = 0) 
  2.  
  3.   { 
  4.   $str = preg_replace("/([\n\r]+)\t+/s","\\1",$str); 
  5.  
  6.   $str = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s""{\\1}",$str); 
  7.  
  8.   $str = preg_replace("/\{template\s+(.+)\}/","<?php include template(\\1); ?>",$str); 相当于 
  9.  
  10.   $str = preg_replace("/\{include\s+(.+)\}/","<?php include \\1; ?>",$str); 
  11.  
  12.   $str = preg_replace("/\{php\s+(.+)\}/","<?php \\1?>",$str); 
  13.  
  14.   $str = preg_replace("/\{if\s+(.+?)\}/","<?php if(\\1) { ?>",$str); 
  15.  
  16.   $str = preg_replace("/\{else\}/","<?php } else { ?>",$str); 
  17.  
  18.   $str = preg_replace("/\{elseif\s+(.+?)\}/","<?php } elseif (\\1) { ?>",$str); 
  19.  
  20.   $str = preg_replace("/\{\/if\}/","<?php } ?>",$str); 
  21.  
  22.   $str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\}/","<?php if(is_array(\\1)) foreach(\\1 AS \\2) { ?>",$str); 
  23.  
  24.   $str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/""<?php if(is_array(\\1)) foreach(\\1 AS \\2 => \\3) { ?>",$str); 
  25.  
  26.   $str = preg_replace("/\{\/loop\}/","<?php } ?>",$str); 
  27.  
  28.   $str = preg_replace("/\{\/get\}/","<?php } unset(\$DATA); ?>",$str); 
  29.  
  30.   $str = preg_replace("/\{tag_([^}]+)\}/e""get_tag('\\1')"$str); 
  31.  
  32.   $str = preg_replace("/\{get\s+([^}]+)\}/e""get_parse('\\1')"$str); 
  33.  
  34.   $str = preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7 f-\xff:]*\(([^{}]*)\))\}/","<?php echo \\1;?>",$str); 
  35.  
  36.   $str = preg_replace("/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_ \x7f-\xff:]*\(([^{}]*)\))\}/","<?php echo \\1;?>",$str); 
  37.  
  38.   $str = preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/","< ?php echo \\1;?>",$str); 
  39.  
  40.   $str = preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/es""addquote('<?php echo \\1;?>')",$str); 
  41.  
  42.   $str = preg_replace("/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff ]*)\}/s""<?php echo \\1;?>",$str); 
  43.  
  44.   if(!$istag$str = "<?php defined('IN_PHPCMS') or exit('Access Denied'); ?>".$str;
  45.  
  46.   return $str
  47.  
  48.   } 

其中先容几个关键点:

/s 是将 代表任意符号的"."变成任意的符号。dedecms \s 是空格的意思.

$str = preg_replace("/([\n\r]+)\t+/s","\\1",$str); 取括号中一个或者多个回车换行的字符并且外面有至少一个制表符的字符.

$str = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}",$str); //这是将注释父里面变成大括号.

$str = preg_replace("/\{include\s+(.+)\}/","<?php include \\1; ?>",$str); //输进的 include 将被替换成 <?php include ?>

$str = preg_replace("/\{php\s+(.+)\}/","<?php \\1?>",$str); //例如 {php echo "a"} 将被替换成 <?php echo "a";?>

$str = preg_replace("/\{if\s+(.+?)\}/","<?php if(\\1) { ?>",$str);

$str = preg_replace("/\{else\}/","<?php } else { ?>",$str); 这个事讲{if } {else} 替换<?php if ($a='1') echo a; else {a='b'}?> 替换的是 if +空格 +一个或者多个字符 if (\\1)是取括号里的值也就是内存地址的值

$str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\}/","<?php if(is_array(\\1)) foreach(\\1 AS \\2) { ?>",$str);

$str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/", "<?php if(is_array(\\1)) foreach(\\1 AS \\2 => \\3) { ?>",$str); // 这连个似乎拿出来有点疏漏http://www.pcbaike.net/,大家可以研究研究,测试时没有合适的主要是取第一个括号中或者第二个括号中的数据假如是数组替换成foreach循环 $str = preg_replace("/\{get\s+([^}]+)\}/e", "get_parse('\\1')", $str);

这个事替换get标签实质上是调用一个 get_parse 函数,有参函数.

 

本文《phpcms 的模板模板机制》发布于PHPCMS管理系统文章,作者:admin,如若转载,请注明出处:https://www.phpcms.vip/help/doc/815.html,否则禁止转载,谢谢配合!