• 问答项目---自定义标签调用户信息!


    标签位置同样在 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;
        }
    };
  • 相关阅读:
    Android--从路径中提取文件名
    Android--全局变量 很好很强大
    Android数据库升级实例
    eclipse中maven项目部署到tomcat [转]
    【项目管理和构建】十分钟教程,eclipse配置maven + 创建maven项目
    maven下载和安装
    Maven 在eclipse中如何配置
    怎么查看eclipse是否支持maven
    证书
    Tomcat7中开启gzip压缩功能的配置方法
  • 原文地址:https://www.cnblogs.com/e0yu/p/7454976.html
Copyright © 2020-2023  润新知