廢話不多說,感覺這個自定義標籤好處就是配置好了能夠快速方便使用。
拿來快速讀取信息簡直不要太完美!
創建自定義標籤文件ProductInfo.php。【路徑:application/common/taglib/ProductInfo.php】
<?php
namespace app\common\taglib;
use app\service\model\DomainModel;
use think\facade\Cache;
use think\template\TagLib;
class ProductInfo extends TagLib
{
protected $tags = [
'price' => ['attr'=>'prodid','close'=>0]
];
/**
* @param $tag 前臺模板傳入數據,array
* @return string
*/
public function tagPrice($tag) {
$productInfo = Cache::get('PRODUCT_INFO');
if (empty($productInfo)) {
$domain = new DomainModel();
$productInfo = $domain->select()->toArray();
Cache::set('PRODUCT_INFO',$productInfo);
}
foreach ($productInfo as $key => $value) {
if ($value['type'] == $tag['prodid']) {
return $value['price'];
}
}
return '未知';
}
}
template配置文件中添加配置信息。【路徑:config/template.php】
//預先加載的標籤庫
'taglib_pre_load' => 'app\common\taglib\ProductInfo',
接下來就是使用了 !
{productinfo:price prodid="domcn"}
{productinfo:price prodid="domcom"}
{productinfo:price prodid="domhzcom"}
是不是很簡單!
每天記錄一點點~