thinkphp自定义模板标签(二)

来源:互联网 发布:中世纪2优化9 圣殿骑士 编辑:程序博客网 时间:2024/05/16 01:34

上篇文章已经把自定义标签的准备工作讲完了;那么接下来就是见证...的时候了;没看如何配置的请先移步thinkphp自定义模板标签(一)

    闭合标签就是单标签;比如a标签、img标签等等;

    非闭合标签就是对标签;比如div、p标签等等;

    这里以自定义的ueditor和recommend标签为例;

    自定义的闭合标签比较适合用来引文件;

    自定义的对标签比较适合调取数据库的数据并前台页面遍历显示;

<?php  namespace Common\Tag;use Think\Template\TagLib;class My extends TagLib {    // 定义标签    protected $tags=array(         // 标签定义: attr 属性列表 close 是否闭合(0 或者1 默认1) alias 标签别名 level 嵌套层次        'ueditor'=> array('attr'=>'name,content','close'=>0),        'recommend'=>array('attr'=>'limit','level'=>1)        );    /**    *引入ueidter编辑器    *@param string $tag  name:表单name content:编辑器初始化后 默认内容    */    public function _ueditor($tag){        $name=$tag['name'];        $content=$tag['content'];        $link=<<<php<script id="container" name="$name" type="text/plain">    $content</script><script type="text/javascript" src="__PUBLIC__/static/ueditor1_4_3/ueditor.config.js"></script><script type="text/javascript" src="__PUBLIC__/static/ueditor1_4_3/ueditor.all.js"></script><script type="text/javascript">    var ue = UE.getEditor('container');</script>php;        return $link;    }    // 置顶推荐文章标签 cid为空时则抓取全部分类下的推荐文章     public function _recommend($tag,$content){         if(empty($tag['cid'])){             $where="is_show=1 and is_delete=0 and is_top=1";         }else{             $where='is_show=1 and is_delete=0 and is_top=1 and cid='.$tag['cid'];         }         $limit=$tag['limit'];         // p($recommend);         $php=<<<php<?php                     \$recommend=M('Article')->field('aid,title')->where("$where")->limit($limit)->select();            foreach (\$recommend as \$k => \$field) {                \$url=U('Home/Index/article',array('aid'=>\$field['aid']));?>php;        $php.=$content;    //拼字符串的过程。。。        $php.='<?php } ?>';//foreach的回扩;        return $php;     }}?>

定义完成后在html页面中就可以直接使用<ueditor />即可引入ueditor文件;

<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <title>Document</title><ueditor /></head><body>    <recommend cid="" limit="10">       <a class="recommend-a" href="{:U('Home/Index/article',array('aid'=>$field['aid']))}" target="_blank">{$k+1}:{$field['title']}</a>    </recommend></body></html>

ueditor标签可以直接实例化ueditor编辑器

而如下图本站的热门推荐就是使用recommend遍历的;

白俊遥博客

0 0
原创粉丝点击