thinkphp3.0 自定义标签
来源:互联网 发布:mac水波纹效果怎么弄 编辑:程序博客网 时间:2024/06/08 15:51
在thinkphp3.0之前,TP的模板引擎是基于xml标签来为TP模板引擎进行扩展,3.0之后TP的模板引擎可以到类中用数组的形式进行定义。要想让TP的模板功能更加的强大,那你就得学会扩展TP的自定义标签功能
TP3.0下面自定义标签步骤如下:
- 文件名的规范;
TagLibModelTag.class.php(TagLib加上你的标签名称,首字母记得要大写)
- 目录所在;
是自定义的标签我将他发到了thinkphp/Extend/COM/TagLib目录下面(为了防止在项目中多处引用该目录,可以在配置文件中直接加载这个目录
'APP_AUTOLOAD_PATH'=>'@.TagLib,COM.TagLib'
- 代码的编写
我以我开发的从model中调用函数返回数据为例
class TagLibModelTag extends TagLib {protected $tags = array(// 标签定义: attr 属性列表 close 是否闭合(0 或者1 默认1) alias 标签别名 level 嵌套层次'select'=>array('attr'=>'name,function,outputhtml,key,item,params','level' => 3));/** * 调用数据模型中的函数 * outputhtml为是否输出html代码(如果标签写在模板文件中就要输出到模板文件中, * 如果为某个模板的模板或内容保存的则要直接输出) */public function _select($attr, $content){$tag = $this->parseXmlAttr($attr, 'select');$outputHtml = !empty($tag['outputhtml']) ? intval($tag['outputhtml']) : 0;if( $tag['name'] && $tag['function'] ){$item = !empty($tag['item']) ? $tag['item'] : 'stnts';$key = !empty($tag['key']) ? $tag['key'] : 'key';$paramStr = !empty($tag['params'])?addslashes($tag['params']):''; if( $outputHtml ){ $tplFileName = LIBRARY_PATH."COM/STNTS/TagLib/modelTag.tpl.php"; if( file_exists($tplFileName) ){ $tpl = file_get_contents($tplFileName); $parsestr = str_replace(array('{modelname}','{modelfunction}','{paramStr}','{key}','{val}','{content}'), array($tag['name'],$tag['function'],$paramStr,$key,$item,$content), $tpl); } }else{ $_model = DE($tag['name']); if( method_exists($_model, $tag['function']) ){ if( $tag['params'] ){$params = explode("|", $tag['params']);foreach( $params as $paramStr ){$paramArray = explode("::", $paramStr);$map[$paramArray[0]] = $paramArray[1];}} $find = preg_match_all('/'.C('taglib_begin')."\\$".$item.'\.(\w*?)'.C('taglib_end').'/is',$content,$matches,PREG_SET_ORDER); if( $find ){ $_result = $_model->$tag['function']($map); foreach ( $_result as $key=>$val ){ $tempCotnent = $content; foreach ( $matches as $m ){ $tempCotnent = str_replace($m[0], $val[$m[1]], $tempCotnent); } $parsestr .= $tempCotnent; } } } }}return $parsestr;}}
注意事项:
- 一定要定义$tags属性,里面要定义key和对应的值, 标签定义: attr 属性列表 close 是否闭合(0 或者1 默认1) alias 标签别名 level 嵌套层次
- 定义方法:方法名称要和$tags中的key对应,并在key的前面加上下划线.列如我在$tags中定义了一个key的值为:mylist。那么定义的方法名称为:_mysql($attr,$content)
- 在函数返回值的时候不用到{}标签还要记得<?php ?>之间的闭合问题.在我的这个类中要返回php代码的,已有直接返回html的,所有有时候可以将php返回的模板放到同一个目录中方便自己修改;如下:
<?php$_model = DE('{modelname}');$_modelFunction = '{modelfunction}';$_paramStr =stripslashes ('{paramStr}');$_key = '{key}';$_val='{val}';if( method_exists($_model,$_modelFunction) ):if( strrpos($_paramStr,"|") || strrpos( $_paramStr,"::") ):$params = explode("|", $_paramStr);foreach ( $params as $paramV ):if( strrpos( $paramV,"::") ):$param = explode("::", $paramV);$map[$param[0]] = $param[1];endif;endforeach;endif;$_result = $_model->$_modelFunction($map);foreach ( $_result as $$_key=> $$_val ):?>{content}<?php endforeach;endif;?>
采用替换的形式来生成对应的php代码,在返回html代码的时候要用正则来解析你的内容如:
$find = preg_match_all('/'.C('taglib_begin')."\\$".$item.'\.(\w*?)'.C('taglib_end').'/is',$content,$matches,PREG_SET_ORDER);
- 一定要有返回结果的字符串
- 在你的模板中定义
{%taglib name="modelTag"/%}{%modelTag:select name="new" function="getData" params="where::id=3"%}{%$stnts.title%}{%$stnts.title%}
注意事项:{%$stnts.intro%}
{%/modelTag:select%}- 一定要加入{%taglib name="modelTag"/%}。name为你的标签类名称
{%modelTag:select name="new" function="getData" params="where::id=3"%}…{%/modelTag:select%}
{%modelTag:select%}中modelTag为你的标签类名称,select为你在modelTag类中定义$tags的key,后面的属性都为在$tags中定义的属性,请一一对应,否则将无妨得到对应的值
- thinkphp3.0 自定义标签
- ThinkPHP3.2+版本如何自定义自己的标签
- thinkphp3.2.3 自定义标签图文教程,Taglib放置项目路径
- thinkphp3.2自定义常量
- ThinkPHP3.2.3自定义函数库
- ThinkPHP3.2 <if>标签嵌套
- thinkphp3.2自定义常量数组
- ThinkPHP3.2.3分页样式自定义
- Thinkphp3.2分页自定义样式
- JSP2.0自定义标签
- JSP2.0自定义标签
- sitemesh3.0 自定义标签
- 【thinkphp3.x】thinkphp3.x中自定义Action、Model及View
- thinkphp3.2标签中数组的使用
- thinkphp3.2 include标签遇到的问题
- ThinkPHP3.0 快捷查询
- thinkphp3.0之控制器
- thinkphp3.0之模型
- 触发器
- 抢占式多任务处理
- 索尼常务董事:绩效主义毁了索尼
- rsync 的核心算法
- 恨自己
- thinkphp3.0 自定义标签
- 学习书籍列表
- CIO:树立IT服务的理念的重要性
- listview的点击事件
- 高手支招,我是继续非IT工作,还是立即辞掉去学习或者进入IT公司??
- 1.1 数据库基本概念
- CIO不应译成首席信息主管
- Android 动态加载布局
- poj 3300 Tour de France