TP3.2.3 亲测自定义标签通用代码

jerry thinkphp 2015年11月19日 收藏
3.2.3自定义标签通用代码(新手第一次发)
标签库代码:
  1. <?php
  2. namespace Think\Template\TagLib;
  3. use Think\Template\TagLib;

  4. class lx extends TagLib {
  5.     protected $tags = array(
  6.     'list' => array('attr' => 'type,order,limit,where','close' => 1),
  7.     );
  8.     public function _list($attr,$content) {
  9.         $type = $attr['type']; //要查询的数据表
  10.         $order = $attr['order'];    //排序
  11.         $limit = $attr['limit']; //多少条数据
  12.         $where = $attr['where']; //查询条件
  13.         $str = '<?php ';
  14.         $str .= '$result = M("' . $type . '")->where("' . $where . '")->order("' . $order . '")->limit(' . $limit . ')->select();';
  15.         $str .= 'foreach ($result as $v):';
  16.         $str .= '?>';
  17.         $str .= $content;
  18.         $str .= '<?php endforeach ?>';
  19.         return $str;
  20.     }
  21. }
调用代码:
  1. <lx:list type="user" limit="" order="id desc" where="user = 'test'">
  2. {$v[id]} -- {$v[user]}<br />
  3. </lx:list>
注意:where 的条件字符串要有单引号,另外{$v[id]}要这样写,如果写成{$v:id}无效.该文件与Cx标签库放在同一个文件夹下...修改THINKPHP文件夹下通用配置文件. 新手第一次发,研究了三天,如果有任何BUG,请及时告知..谢谢.