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;
        }
    };
  • 相关阅读:
    284. Peeking Iterator
    283. Move Zeroes
    282. Expression Add Operators
    281. Zigzag Iterator
    280. Wiggle Sort
    279. Perfect Squares
    python 正则匹配替换,在匹配的字符后方添加新的字符
    odoo default_get 方法和onchange装饰器造成冲突,
    redmine 如何启用用户图标
    odoo 打印执行的sql语句
  • 原文地址:https://www.cnblogs.com/e0yu/p/7454976.html
Copyright © 2011-2022 走看看