自己弄得一个简单的标签扩展,可以参考下

浏览:719 发布日期:2015/07/20 分类:技术分享
提供类似的方法,大家自已扩展,我也是找了很多资料才弄出来,费了老大劲<?php
/**
 * Created by PhpStorm.
 * User: zhibo
 * 15-7-14 下午10:56
 * Mail:ni5400@163.com
 * Web:http://www.ki35.com
 */
namespace Think\Template\TagLib;
use MyLibrary\Category;
use Think\Template\TagLib;
//自定义栏目标签库
class Ki35 extends TagLib{
    // 标签定义
    protected $tags  = array(
        //close 1是闭合标签,0是短标签  attr是定义包含的属性
        'category' =>array('attr'=>'limit,order','close'=>1),
        'nature' =>array('attr'=>'limit,order,nature_id,status,','close'=>1),
        'list' =>array('attr'=>'limit,order','close'=>1),
        'prev' =>array('attr'=>'limit,order','close'=>1),
        'next' =>array('attr'=>'limit,order','close'=>1),
    );
    //查询导航,及二级导航
    public function _category($tag,$content){
        $limit     = $tag['limit'];
        $order    = $tag['order'];
        $str='<?php ';
        $str.='$cateList=M("Category")->select();';
        $str.='$Category=new MyLibrary\Category();';
        $str.='$cateList=$Category->cate_ullist($cateList);';
        $str.='foreach($cateList as $cateList_v) :';
        $str.='extract($cateList_v);';
        $str.='$url=U("/"."Blog/category_".$id)';
        $str.='?>';
        $str.=$content;
        $str.='<?php endforeach;?>';
        return $str;
    }

    //查询某推荐位下的多条信息
    public function _nature($tag,$content){
        $naturelimit     = $tag['limit'];
        $natureorder     = $tag['order'];
        $nature_id       =$tag['nature_id'];
        $status   =$tag['status'];
        $str='<?php ';
        $str.='$natruemap["nature_id"]='.$nature_id.';';
        $str.='$natruemap["status"]='.$status.';';
        $str.='$natureList=D("NatureView")->where($natruemap)->limit("'.$naturelimit.'")->order("'.$natureorder.'")->select();';
        $str.='foreach($natureList as $natureList_v) :';
        $str.='extract($natureList_v);';
        $str.='$url=U("/"."Blog/notes/".$id)';
        $str.='?>';
        $str.=$content;
        $str.='<?php endforeach;?>';
        return $str;
    }

    //获取当前栏目的list列表,如果当前栏目不存在则获取所有栏目
    public function _list($tag,$content){
        $limit    = $tag['limit'];
        $order    = $tag['order'];
        $str='<?php ';
        $str.='if(I("cate_id","","intval"))  $thisMap["catid"]=array("eq",I("cate_id","intval"));';
        $str.='$aList=M("Article")->field("id,title,hits,add_time,update_time")->where($thisMap)->limit("'.$limit.'")->order("'.$order.'")->select();';
        $str.='foreach($aList as $aList_v) :';
        $str.='extract($aList_v);';
        $str.='$url=U("/"."Blog/notes/".$id)';
        $str.='?>';
        $str.=$content;
        $str.='<?php endforeach;?>';
        return $str;
    }

    //获取上一/几条数据
    public function _prev($tag,$content){
        $limit    = $tag['limit'];
        $str='<?php ';
        $str.='if(I("id","","intval"))  $thisMap["id"]=array("lt",I("id","intval"));';
        $str.='$prevList=M("Article")->field("id,title")->where($thisMap)->limit("'.$limit.'")->order("id Desc")->select();';
        $str.='foreach($prevList as $prevList_v) :';
        $str.='extract($prevList_v);';
        $str.='$url=U("/"."Blog/notes/".$id)';
        $str.='?>';
        $str.=$content;
        $str.='<?php endforeach;?>';
        return $str;
    }
    //获取下一/几条数据
    public function _next($tag,$content){
        $limit    = $tag['limit'];
        $str='<?php ';
        $str.='if(I("id","","intval"))  $thisMap["id"]=array("gt",I("id","intval"));';
        $str.='$nextList=M("Article")->field("id,title")->where($thisMap)->limit("'.$limit.'")->select();';
        $str.='foreach($nextList as $nextList_v) :';
        $str.='extract($nextList_v);';
        $str.='$url=U("/"."Blog/notes/".$id)';
        $str.='?>';
        $str.=$content;
        $str.='<?php endforeach;?>';
        return $str;
    }

}
使用案例:<{ki35:nature order="update_time DESC" nature_id="5" limit="5" status="1"}>
                        <li class="clearfix">
                            <div class="blog-new-article-list-img"><a href="<{$url}>"><img src="<{if condition='$thumb'}><{$thumb}><{else /}>/Public/Blog/images/550d30755870c.png<{/if}>" alt="<{$title}>" width="96" height="54"></a></div>
                            <div class="blog-new-atticle-list-digest">
                                <h3><a href="<{$url}>"><{$title}></a></h3>
                                <p><{$introduce}></p>
                            </div>
                        </li>
                        <{/ki35:nature}>
最佳答案
评论() 相关
后面还有条评论,点击查看>>