如下图<eq name=”uid” value=””>未登录状态下<el />登录状态下 </eq>
name为条件 和value为值
thinkphp/thinkphp/library/think/template/taglib/cx.class.php
<?php// +----------------------------------------------------------------------// | thinkphp [ we can do it just think it ]// +----------------------------------------------------------------------// | copyright (c) 2006-2014 /d/file/titlepic/www.thinkphp.cn all rights rerved.// +----------------------------------------------------------------------// | licend ( http://www.apache.org/licens/licen-2.0 )// +----------------------------------------------------------------------// | author: liu21st <liu21st@gmail.com>// +----------------------------------------------------------------------namespace think\template\taglib;u think\template\taglib;/*** cx标签库解析类*/class cx extends taglib{// 标签定义protected $tags = array(// 标签定义: attr 属性列表 clo 是否闭合(0 或者1 默认1) alias 标签别名 level 嵌套层次'php' => array(),'volist' => array('attr' => 'name,id,offt,length,key,mod', 'level' => 3, 'alias' => 'iterate'),'foreach' => array('attr' => 'name,item,key', 'level' => 3),'if' => array('attr' => 'condition', 'level' => 2),'elif' => array('attr' => 'condition', 'clo' => 0),'el' => array('attr' => '', 'clo' => 0),'switch' => array('attr' => 'name', 'level' => 2),'ca' => array('attr' => 'value,break'),'default' => array('attr' => '', 'clo' => 0),'compare' => array('attr' => 'name,value,type', 'level' => 3, 'alias' => 'eq,equal,notequal,neq,gt,lt,egt,elt,heq,nheq'),'range' => array('attr' => 'name,value,type', 'level' => 3, 'alias' => 'in,notin,between,notbetween'),'empty' => array('attr' => 'name', 'level' => 3),'notempty' => array('attr' => 'name', 'level' => 3),'prent' => array('attr' => 'name', 'level' => 3),'notprent' => array('attr' => 'name', 'level' => 3),'defined' => array('attr' => 'name', 'level' => 3),'notdefined' => array('attr' => 'name', 'level' => 3),'import' => array('attr' => 'file,href,type,value,bapath', 'clo' => 0, 'alias' => 'load,css,js'),'assign' => array('attr' => 'name,value', 'clo' => 0),'define' => array('attr' => 'name,value', 'clo' => 0),'for' => array('attr' => 'start,end,name,comparison,step', 'level' => 3),);/*** php标签解析* @access public* @param array $tag 标签属性* @param string $content 标签内容* @return string*/public function _php($tag, $content){$parstr = '<?php ' . $content . ' ?>';return $parstr;}/*** volist标签解析 循环输出数据集* 格式:* <volist name="urlist" id="ur" empty="" >* {ur.urname}* {ur.email}* </volist>* @access public* @param array $tag 标签属性* @param string $content 标签内容* @return string|void*/public function _volist($tag, $content){$name = $tag['name'];$id = $tag['id'];$empty = ist($tag['empty']) ? $tag['empty'] : '';$key = !empty($tag['key']) ? $tag['key'] : 'i';$mod = ist($tag['mod']) ? $tag['mod'] : '2';// 允许使用函数设定数据集 <volist name=":fun('arg')" id="vo">{$vo.name}</volist>$parstr = '<?php ';if (0 === strpos($name, ':')) {$parstr .= '$_result=' . substr($name, 1) . ';';$name = '$_result';} el {$name = $this->autobuildvar($name);}$parstr .= 'if(is_array(' . $name . ')): $' . $key . ' = 0;';if (ist($tag['length']) && '' != $tag['length']) {$parstr .= ' $__list__ = array_slice(' . $name . ',' . $tag['offt'] . ',' . $tag['length'] . ',true);';} elif (ist($tag['offt']) && '' != $tag['offt']) {$parstr .= ' $__list__ = array_slice(' . $name . ',' . $tag['offt'] . ',null,true);';} el {$parstr .= ' $__list__ = ' . $name . ';';}$parstr .= 'if( count($__list__)==0 ) : echo "' . $empty . '" ;';$parstr .= 'el: ';$parstr .= 'foreach($__list__ as $key=>$' . $id . '): ';$parstr .= '$mod = ($' . $key . ' % ' . $mod . ' );';$parstr .= '++$' . $key . ';?>';$parstr .= $this->tpl->par($content);$parstr .= '<?php endforeach; endif; el: echo "' . $empty . '" ;endif; ?>';if (!empty($parstr)) {return $parstr;}return;}/*** foreach标签解析 循环输出数据集* @access public* @param array $tag 标签属性* @param string $content 标签内容* @return string|void*/public function _foreach($tag, $content){$name = $tag['name'];$item = $tag['item'];$key = !empty($tag['key']) ? $tag['key'] : 'key';$name = $this->autobuildvar($name);$parstr = '<?php if(is_array(' . $name . ')): foreach(' . $name . ' as $' . $key . '=>$' . $item . '): ?>';$parstr .= $this->tpl->par($content);$parstr .= '<?php endforeach; endif; ?>';if (!empty($parstr)) {return $parstr;}return;}/*** if标签解析* 格式:* <if condition=" $a eq 1" >* <elif condition="$a eq 2" />* <el />* </if>* 表达式支持 eq neq gt egt lt elt == > >= < <= or and || &&* @access public* @param array $tag 标签属性* @param string $content 标签内容* @return string*/public function _if($tag, $content){$condition = $this->parcondition($tag['condition']);$parstr = '<?php if(' . $condition . '): ?>' . $content . '<?php endif; ?>';return $parstr;}/*** el标签解析* 格式:见if标签* @access public* @param array $tag 标签属性* @param string $content 标签内容* @return string*/public function _elif($tag, $content){$condition = $this->parcondition($tag['condition']);$parstr = '<?php elif(' . $condition . '): ?>';return $parstr;}/*** el标签解析* @access public* @param array $tag 标签属性* @return string*/pub七年级下册英语期中lic function _el($tag){$parstr = '<?php el: ?>';return $parstr;}/*** switch标签解析* 格式:* <switch name="a.name" >* <ca value="1" break="fal">1</ca>* <ca value="2" >2</ca>* <default />other* </switch>* @access public* @param array $tag 标签属性* @param string $content 标签内容* @return string*/public function _switch($tag, $content){$name = $tag['name'];$vararray = explode('|', $name);$name = array_shift($vararray);$name = $this->autobuildvar($name);if (count($vararray) > 0) {$name = $this->tpl->parvarfunction($name, $vararray);}$parstr = '<?php switch(' . $name . '): ?>' . $content . '<?php endswitch;?>';return $parstr;}/*** ca标签解析 需要配合switch才有效* @access public* @param array $tag 标签属性* @param string $content 标签内容* @return string*/public function _ca($tag, $content){$value = $tag['value'];if ('$' == substr($value, 0, 1)) {$vararray = explode('|', $value);$value = array_shift($vararray);$value = $this->autobuildvar(substr($value, 1));if (count($vararray) > 0) {$value = $this->tpl->parvarfunction($value, $vararray);}$value = 'ca ' . $value . ': ';} elif (strpos($value, '|')) {$values = explode('|', $value);$value = '';foreach ($values as $val) {$value .= 'ca "' . addslashes($val) . '": ';}} el {$value = 'ca "' . $value . '": ';}$parstr = '<?php ' . $value . ' ?>' . $content;$isbreak = ist($tag['break']) ? $tag['break'] : '';if ('' == $isbreak || $isbreak) {$parstr .= '<?php break;?>';}return $parstr;}/*** default标签解析 需要配合switch才有效* 使用: <default /&gmake sbt;ddfdf* @access public* @param array $tag 标签属性* @param string $content 标签内容* @return string*/public function _default($tag){$parstr = '<?php default: ?>';return $parstr;}/*** compare标签解析* 用于值的比较 支持 eq neq gt lt egt elt heq nheq 默认是eq* 格式: <compare name="" type="eq" value="" >content</compare>* @access public* @param array $tag 标签属性* @param string $content 标签内容* @return string*/public function _compare($tag, $content, $type = 'eq'){$name = $tag['name'];$value = $tag['value'];$type = ist($tag['type']) ? $tag['type'] : $type;$type = $this->parcondition(' ' . $type . ' ');$vararray = explode('|', $name);$name = array_shift($vararray);$name = $this->autobuildvar($name);if (count($vararray) > 0) {$name = $this->tpl->parvarfunction($name, $vararray);}if ('$' == substr($value, 0, 1)) {$value = $this->autobuildvar(substr($value, 1));} el {$value = '"' . $value . '"';}$parstr = '<?php if((' . $name . ') ' . $type . ' ' . $value . '): ?>' . $content . '<?php endif; ?>';return $parstr;}public function _eq($tag, $content){return $this->_compare($tag, $content, 'eq');}public function _equal($tag, $content){return $this->_compare($tag, $content, 'eq');}public function _neq($tag, $content){return $this->_compare($tag, $content, 'neq');}public function _notequal($tag, $content){return $this->_compare($tag, $content, 'neq');}public function _gt($tag, $content){return $this->_compare($tag, $content, 'gt');}public function _lt($tag, $content){return $this->_compare($tag, $content, 'lt');}public function _egt($tag, $content){return $this->_compare($tag, $content, 'egt');}public function _elt($tag, $content){return $this->_compare($tag, $content, 'elt');}public function _heq($tag, $content){return $this->_compare($tag, $content, 'heq');}public function _nheq($tag, $content){return $this->_compare($tag, $content, 'nheq');}/*** range标签解析* 如果某个变量存在于某个范围 则输出内容 type= in 表示在范围内 否则表示在范围外* 格式: <range name="var|function" value="val" type='in|notin' >content</range>* example: <range name="a" value="1,2,3" type='in' >content</range>* @access public* @param array $tag 标签属性* @param string $content 标签内容* @param string $type 比较类型* @return string*/public function _range($tag, $content, $type = 'in'){$name = $tag['name'];$value = $tag['value'];$vararray = explode('|', 什么是双学位$name);$name = array_shift($vararray);$name = $this->autobuildvar($name);if (count($vararray) > 0) {$name = $this->tpl->parvarfunction($name, $vararray);}$type = ist($tag['type']) ? $tag['type'] : $type;if ('$' == substr($value, 0, 1)) {$value = $this->autobuildvar(substr($value, 1));$str = 'is_array(' . $value . ')?' . $value . ':explode(\',\',' . $value . ')';} el {$value = '"' . $value . '"';$str = 'explode(\',\',' . $value . ')';}if ('between' == $type) {$parstr = '<?php $_range_var_=' . $str . ';if(' . $name . '>= $_range_var_[0] && ' . $name . '<= $_range_var_[1]):?>' . $content . '<?php endif; ?>';} elif ('notbetween' == $type) {$parstr = '<?php $_range_var_=' . $str . ';if(' . $name . '<$_range_var_[0] || ' . $name . '>$_range_var_[1]):?>' . $content . '<?php endif; ?>';} el {$fun = ('in' == $type) ? 'in_array' : '!in_array';$parstr = '<?php if(' . $fun . '((' . $name . '), ' . $str . ')): ?>' . $content . '<?php endif; ?>';}return $parstr;}// range标签的别名 用于in判断public function _in($tag, $content){return $this->_range($tag, $content, 'in');}// range标签的别名 用于notin判断public function _notin($tag, $content){return $this->_range($tag, $content, 'notin');}public function _between($tag, $content){return $this->_range($tag, $content, 'between');}public function _notbetween($tag, $content){return $this->_range($tag, $content, 'notbetween');}/*** prent标签解析* 如果某个变量已经设置 则输出内容* 格式: <prent name="" >content</prent>* @access public* @param array $tag 标签属性* @param string $content 标签内容* @return string*/public function _prent($tag, $content){$name = $tag['name'];$name = $this->autobuildvar($name);$parstr = '<?php if(ist(' . $name . ')): ?>' . $conten矜持不苟的意思t . '<?php endif; ?>';return $parstr;}/*** notprent标签解析* 如果某个变量没有设置,则输出内容* 格式: <notprent name="" >content</notprent>* @access public* @param array $tag 标签属性* @param string $content 标签内容* @return string*/public function _notprent($tag, $content){$name = $tag['name'];$name = $this->autobuildvar($name);$parstr = '<?php if(!ist(' . $name . ')): ?>' . $content . '<?php endif; ?>';return $parstr;}/*** empty标签解析* 如果某个变量为empty 则输出内容* 格式: <empty name="" >content</empty>* @access public* @param array $tag 标签属性* @param string $content 标签内容* @return string*/public function _empty($tag, $content){$name = $tag['name'];$name = $this->autobuildvar($name);$parstr = '<?php if(empty(' . $name . ')): ?>' . $content . '<?php endif; ?>';return $parstr;}public function _notempty($tag, $content){$name = $tag['name'];$name = $this->autobuildvar($name);$parstr = '<?php if(!empty(' . $name . ')): ?>' . $content . '<?php endif; ?>';return $parstr;}/*** 判断是否已经定义了该常量* <defined name='txt'>已定义</defined>* @param <type> $attr* @param <type> $content* @return string*/public function _defined($tag, $content){$name = $tag['name'];$parstr = '<?php if(defined("' . $name . '")): ?>' . $content . '<?php endif; ?>';return $parstr;}public function _notdefined($tag, $content){$name = $tag['name'];$parstr = '<?php if(!defined("' . $name . '")): ?>' . $content . '<?php endif; ?>';return $parstr;}/*** import 标签解析 <import file="js.ba" />* <import file="css.ba" type="css" />* @access public* @param array $tag 标签属性* @param string $content 标签内容* @param boolean $isfile 是否文件方式* @param string $type 类型* @return string*/public function _import($tag, $content, $isfile = fal, $type = ''){$file = ist($tag['file']) ? $tag['file'] : $tag['href'];$parstr = '';$endstr = '';// 判断是否存在加载条件 允许使用函数判断(默认为ist)if (ist($tag['value'])) {$vararray = explode('|', $tag['value']);$name = array_shift($vararray);$name = $this->autobuildvar($name);if (!empty($vararray)) {$name = $this->tpl->parvarfunction($name, $vararray);} el {$name = 'ist(' . $name . ')';}$parstr .= '<?php if(' . $name . '): ?>';$endstr = '<?php endif; ?>';}if ($isfile) {// 根据文件名后缀自动识别$type = $type ? $type : (!empty($tag['type']) ? strtolower($tag['type']) : null);// 文件方式导入$array = explode(',', $file);foreach ($array as $val) {if (!$type || ist($ret)) {$type = $ret = strtolower(substr(strrchr($val, '.'), 1));}switch ($type) {ca 'js':$parstr .= '<script type="text/javascript" src="' . $val . '"></script>';break;ca 'css':$parstr .= '<link rel="stylesheet" type="text/css" href="' . $val . '" />';break;ca 'php':$parstr .= '<?php require_cache("' . $val . '"); ?>';break;}}} el {// 命名空间导入模式 默认是js$type = $type ? $type : (!empty($tag['type']) ? strtolower($tag['type']) : 'js');$bapath = !empty($tag['bapath']) ? $tag['bapath'] : __root__ . '/public';// 命名空间方式导入外部文件$array = explode(',', $file);foreach ($array as $val) {if (strpos($val, '?')) {list($val, $version) = explode('?', $val);} el {$version = '';}switch ($type) {ca 'js':$parstr .= '<script type="text/javascript" src="' . $bapath . '/' . str_replace(array('.', '#'), array('/', '.'), $val) . '.js' . ($version ? '?' . $version : '') . '"></script>';break;ca 'css':$parstr .= '<link rel="stylesheet" type="text/css" href="' . $bapath . '/' . str_replace(array('.', '#'), array('/', '.'), $val) . '.css' . ($version ? '?' . $version : '') . '" />';break;ca 'php':$parstr .= '<?php import("' . $val . '"); ?>';break;}}}return $parstr . $endstr;}// import别名 采用文件方式加载(要使用命名空间必须用import) 例如 <load file="__public__/js/ba.js" />public function _load($tag, $content){return $this->_import($tag, $content, true);}// import别名使用 导入css文件 <css file="__public__/css/ba.css" />public function _css($tag, $content){return $this->_import($tag, $content, true, 'css');}// import别名使用 导入js文件 <js file="__public__/js/ba.js" />public function _js($tag, $content){return $this->_import($tag, $content, true, 'js');}/*** assign标签解析* 在模板中给某个变量赋值 支持变量赋值* 格式: <assign name="" value="" />* @access public* @param array $tag 标签属性* @param string $content 标签内容* @return string*/public function _assign($tag, $content){$name = $this->autobuildvar($tag['name']);if ('$' == substr($tag['value'], 0, 1)) {$value = $this->autobuildvar(substr($tag['value'], 1));} el {$value = '\'' . $tag['value'] . '\'';}$parstr = '<?php ' . $name . ' = ' . $value . '; ?>';return $parstr;}/*** define标签解析* 在模关于环保的作文题目板中定义常量 支持变量赋值* 格式: <define name="" value="" />* @access public* @param array $tag 标签属性* @param string $content 标签内容* @return string*/public function _define($tag, $content){$name = '\'' . $tag['name'] . '\'';if ('$' == substr($tag['value'], 0, 1)) {$value = $this->autobuildvar(substr($tag['value'], 1));} el {$value = '\'' . $tag['value'] . '\'';}$parstr = '<?php define(' . $name . ', ' . $value . '); ?>';return $parstr;}/*** for标签解析* 格式: <for start="" end="" comparison="" step="" name="" />* @access public* @param array $tag 标签属性* @param string $content 标签内容* @return string*/public function _for($tag, $content){//设置默认值$start = 0;$end = 0;$step = 1;$comparison = 'lt';$name = 'i';$rand = rand(); //添加随机数,防止嵌套变量冲突//获取属性foreach ($tag as $key => $value) {$value = trim($value);if (':' == substr($value, 0, 1)) {$value = substr($value, 1);} elif ('$' == substr($value, 0, 1)) {$value = $this->autobuildvar(substr($value, 1));}switch ($key) {ca 'start':$start = $value;break;ca 'end':$end = $value;break;ca 'step':$step = $value;break;ca 'comparison':$comparison = $value;break;ca 'name':$name = $value;break;}}$parstr = '<?php $__for_start_' . $rand . '__=' . $start . ';$__for_end_' . $rand . '__=' . $end . ';';$parstr .= 'for($' . $name . '=$__for_start_' . $rand . '__;' . $this->parcondition('$' . $name . ' ' . $comparison . ' $__for_end_' . $rand . '__') . ';$' . $name . '+=' . $step . '){ ?>';$parstr .= $content;$parstr .= '<?php } ?>';return $parstr;}}
本文发布于:2023-04-07 12:26:17,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/03fd2e5d79863e38d7525dc1296255fe.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Thinkphp 模板中 if 嵌套层级过多的问题,嵌套3级就报错,取消层级限制.doc
本文 PDF 下载地址:Thinkphp 模板中 if 嵌套层级过多的问题,嵌套3级就报错,取消层级限制.pdf
留言与评论(共有 0 条评论) |