thinkphp自定义模板标签(二)
上篇文章已经把自定义标签的准备工作讲完了;那么接下来就是见证...的时候了;没看如何配置的请先移步thinkphp自定义模板标签(一)
闭合标签就是单标签;比如a标签、img标签等等;
非闭合标签就是对标签;比如div、p标签等等;
这里以自定义的ueditor和recommend标签为例;
自定义的闭合标签比较适合用来引文件;
自定义的对标签比较适合调取数据库的数据并前台页面遍历显示;
<?phpnamespaceCommon\Tag;useThink\Template\TagLib;classMyextendsTagLib{//定义标签protected$tags=array(//标签定义:attr属性列表close是否闭合(0或者1默认1)alias标签别名level嵌套层次'ueditor'=>array('attr'=>'name,content','close'=>0),'recommend'=>array('attr'=>'limit','level'=>1));/***引入ueidter编辑器*@paramstring$tagname:表单namecontent:编辑器初始化后默认内容*/publicfunction_ueditor($tag){$name=$tag['name'];$content=$tag['content'];$link=<<<php<scriptid="container"name="$name"type="text/plain">$content</script><scripttype="text/javascript"src="__PUBLIC__/static/ueditor1_4_3/ueditor.config.js"></script><scripttype="text/javascript"src="__PUBLIC__/static/ueditor1_4_3/ueditor.all.js"></script><scripttype="text/javascript">varue=UE.getEditor('container');</script>php;return$link;}//置顶推荐文章标签cid为空时则抓取全部分类下的推荐文章publicfunction_recommend($tag,$content){if(empty($tag['cid'])){$where="is_show=1andis_delete=0andis_top=1";}else{$where='is_show=1andis_delete=0andis_top=1andcid='.$tag['cid'];}$limit=$tag['limit'];//p($recommend);$php=<<<php<?php\$recommend=M('Article')->field('aid,title')->where("$where")->limit($limit)->select();foreach(\$recommendas\$k=>\$field){\$url=U('Home/Index/article',array('aid'=>\$field['aid']));?>php;$php.=$content;//拼字符串的过程。。。$php.='<?php}?>';//foreach的回扩;return$php;}}?>
定义完成后在html页面中就可以直接使用<ueditor />即可引入ueditor文件;
<!DOCTYPEhtml><html><head><metacharset="UTF-8"><title>Document</title><ueditor/></head><body><recommendcid=""limit="10"><aclass="recommend-a"href="{:U('Home/Index/article',array('aid'=>$field['aid']))}"target="_blank">{$k+1}:{$field['title']}</a></recommend></body></html>
ueditor标签可以直接实例化ueditor编辑器
而如下图本站的热门推荐就是使用recommend遍历的;
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。