thinkphp 3 2自定義標籤

2021-07-31 15:37:39 字數 1884 閱讀 3510

1配置標籤:

在配置檔案中加入
'taglib_build_in'=>  

'cx,webshop',

// webshop 為自定義標籤類名稱

2編寫自定義標籤類:

在thinkphp3.2 taglib目錄加入webshop類,本自定義標籤模仿tpshop,該類**如下namespace think\template\taglib;

use think\template\taglib;

/** * 自定義標籤

*/class webshop extends taglib

$where = $tag['where']; //查詢條件

$item = !empty($tag['item']) ? $tag['item'] : 'item';// 返回的變數item

$key = !empty($tag['key']) ? $tag['key'] : 'key';// 返回的變數key

$pid = !empty($tag['pid']) ? $tag['pid'] : '0';// 返回的變數key

$str = '<?php ';

$str .= '$pid ='.$pid.';';

$str .= '$result = m("banner")->field("bn.id,media_type,ad_name,ad_link,ad_code,link_man,link_email,link_phone,click_count,target,bgcolor,is_wap,ad_width,ad_height,position_style,is_open")

->table("__banner__ bn,__banner_column__ bc")->where("bn.pid = bc.id and pid=$pid and is_show = 1 and is_open = 1")

->order("bn.orderby asc,id")->cache(true,webshop_cache_time)->limit("'.$limit.'")->select();';

$str .= '

$c = '.$limit.'- count($result); // 如果要求數量 和實際數量不一樣 新增預設廣告

if($c > 0)

}else}}

';$str .= 'foreach($result as $'.$key.'=>$'.$item.'):?>';

$str .= $this->tpl->parse($content);

$str .= '<?php endforeach; ?>';

return $str;

}

3自定義標籤說明:
該自定義標籤關聯了兩個表banner 廣告位表,bannercolumn廣告表,廣告位是廣告的分類表,廣告由廣告位限制。
4自定義標籤使用
target="_blank">

該自定義標籤為adv,pid為廣告位分類id,limit為顯示數量位12,type=2為多圖顯示。

ThinkPHP自定義標籤

研究thinkphp自定義標籤的時候,發現網上很多教程都很亂,官網又沒有給出比較詳細的方法,所以就自己研究了一下,現在將thinkphp的方法詳細的記錄下來。1.建立tp的專案,略過 3.在專案名稱 lib中建立taglib資料夾 4.在taglib資料夾中新建乙個taglibxl.class.ph...

thinkphp自定義模板標籤 一

thinkphp內建的foreach和include等模板標籤使用是非常方便的 但是內建的那些標籤只能滿足常用功能,個性化的功能就需要我們自己編寫自定義模板標籤了 下面就是要講解如何實現 示例環境 thinkphp3.2.3 thinkphp的模板標籤放在thinkphp library think...

thinkphp5 自定義標籤

use think template taglib class tytag extends taglib else if empty tag help html return html textarea 表單 label 表單標題 name 表單name value 表單值 help 表單說明 pu...