问答项目---自定义标签调用户信息!
标签位置同样在 My.class.php
<?php namespace Common\Tag; use Think\Template\TagLib; 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; } };