zoukankan      html  css  js  c++  java
  • 问答项目---自定义标签调用户信息!

    标签位置同样在 My.class.php

    <?php
    namespace CommonTag;
    use ThinkTemplateTagLib;
    class My extends TagLib{
        // 定义标签
        // 参考文章 : http://www.thinkphp.cn/topic/34758.html
        protected $tags = array(
            'topcates'=> array('attr'=>'limit'),
            'userinfo'=> array('attr'=>'uid'),
            'location'=> array('attr'=>'cid')
        );
        public function _topcates($attr,$content){
            //调用父类
            $limit = isset($attr['limit']) ? $attr['limit'] : '';
            // 组合PHP代码的字符串
            $str = '<?php ';
            $str .= '$where = array("pid" =>0);';
            $str .= '$_topcatesResult = M("category")->where($where)->limit('.$limit.')->select();';
            $str .= 'foreach($_topcatesResult as $v):';
            $str .= 'extract($v);?>';
            $str .= $content;
            $str .= '<?php endforeach;?>';
            return $str;
        }
        // 提取用户信息的标签
        public function _userinfo($attr,$content){
            $uid = $attr['uid'];
            $str = <<<str
    <?php
        $field = array('id','username','face','answer','adopt','ask','point','exp');
        $_userinfoResult = M('user')->field($field)->find({$uid});
        extract($_userinfoResult);//写如字符表
        $face = empty($face) ? '/Public/Images/noface.gif' : '/Uploads/Face/'.$face;
        $face = '__ROOT__'.$face;
        $adopt = floor($adopt / $answer).'%';//采纳率
        $level = exp_to_level($exp);
    ?>
    str;
        $str.=$content;
        return $str;
        }
        // location 标签
        public function _location($attr,$content){
            $cid = $attr['cid'];
            $str = <<<str
    <?php 
        $cid = {$cid};
        if(S('location_'.$cid)){
            $_location_result = S('location_' . $cid);
        }else{
            $_location_category = M('category')->select();
            $_location_result = array_reverse(get_all_parent($_location_category,$cid));
            S('location_'.$cid);
        }
        foreach($_location_result as $v):
            extract($v);
    ?>
    str;
        $str .= $content;
        $str .= '<?php endforeach; ?>';
        return $str;
        }
    };
  • 相关阅读:
    Ubuntu 16.04 OneDrive自动同步
    在conda环境中pip使用清华源秒速安装skimage、opencv、tensorflow、pytorch1.2.0等p
    写论文的最佳实践
    训练误差、测试误差、泛化误差的区别
    输入法 ctrl+句号 切换 中英文符号
    理解Graham扫描算法 查找凸包
    PDF阅读器 SumatraPDF 设置:电子书字体字号的更换及行距设置
    友情链接
    CRC全套~~~ 转载
    mysql插入中文出错,提示1366
  • 原文地址:https://www.cnblogs.com/e0yu/p/7454976.html
Copyright © 2011-2022 走看看