thinphp 自定义标签,非常方便在模板读取数据。

thinphp 自定义标签的路径和标签类名如下:

BW)GRVXL~DM1K$`LA@SSJ0A

thinphp自定义标签源码如下:

<?php
/**
* Indextab.class.php
* ==============================================
* www.wuhanphper.com 版权所有 2015-2016
* ==============================================
* @date: 2015-2-1 下午9:22:35
* @author: QQ:826877189 Email:[email protected]
* @version:本程序基于thinkphp3.2版本开发
*/
namespace Think\Template\TagLib;
use Think\Template\TagLib;

class Indextab extends TagLib {
	protected $tags = array(
			'list' => array('attr' => 'type,order,limit,where','close' => 1),
	);
	public function _list($attr,$content) {
		$type = $attr['type']; //要查询的数据表
		$order = $attr['order'];    //排序
		$limit = $attr['limit']; //多少条数据
		$where = $attr['where']; //查询条件
		$str = '<?php ';
		$str .= '$result = M("' . $type . '")->where("' . $where . '")->order("' . $order . '")->limit(' . $limit . ')->select();';
		$str .= 'foreach ($result as $indextab):';
		$str .= '?>';
		$str .= $content;
		$str .= '<?php endforeach ?>';
		return $str;
	}
}
?>

thinkphp自定义标签在模板的使用方法与foreach标签和volist标签一样简单,且能套用使用。

注意:where 条件如果是字符串要有单引号如 where=”test = ‘test'”

例如:

<indextab:list type="article" limit="7" order="update_time desc" where="cateid = 97">
								<li><span class="date">{$indextab[create_time|date="y-m-d",###]}</span><a href="{:U('Article/read',array('id'=>$indextab[id]))}">{$indextab[title]}</a></li>
							</indextab:list>

还要去thinkphp下面的配置文件添加额外标签
D:\wamp\wamp\www\caigou150613\ThinkPHP\Conf\convention.php
 ‘TAGLIB_PRE_LOAD’       =>  ‘Widgettab,Indextab’,   // 需要额外加载的标签库(须指定标签库名称),多个以逗号分隔 

转载请注明:PHP博客 » thinphp 自定义标签,非常方便在模板读取数据。

你可能感兴趣的:(thinphp 自定义标签,非常方便在模板读取数据。)