thinkphp自定义模板标签(二)

上篇文章已经把自定义标签的准备工作讲完了;那么接下来就是见证...的时候了;没看如何配置的请先移步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遍历的;

thinkphp自定义模板标签(二)

网页名称:thinkphp自定义模板标签(二)
分享路径:https://www.cdcxhl.com/article26/jodgjg.html

成都网站建设公司_创新互联,为您提供品牌网站设计GoogleChatGPT企业建站小程序开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

营销型网站建设