tp6自定义标签taglib
taotaoit ThinkPHP6 2022-03-23 1873 0
关于本站

“最难不过坚持”

本人承接扒站仿站,php网站维护,病毒查杀,网站编辑,网站改版,html制作

有需要网站维护,改版,病毒查杀,网站编辑,网站备案,html制作等相关的工作可以联系我。
本人有多年相关工作经验,也可提供免费咨询,交个朋友。
有需要探讨问题的朋友,也可以加我微信,共同探讨!
微信:15011482830 QQ:408917339

7440958 2655 39
最新评论
https://jueru.net/
评 留言
:weixiao:
评 留言
:shuijiao: :weiqu: :zhenbang: :leng:
评 留言
:yiwen: :yiwen: :yiwen: :yiwen:
评 EasySass: could not generate CSS file. See Output panel for details.
这个业务逻辑多少都有点奇怪了,阅读浏览次数增值在新闻详情页的控制器方法里setInc,这怎么还写进模型事件里了。如果非要用onAfterRead也可以,把新闻文章的内容单独分出来一个news_content表,然后把它和news做关联,然后给news_content表的onAfterRead事件做增值处理,这样点进新闻页内查询到文章内容时才会触发它。
评 TP6模型事件-查询后onAfterRead不好用
文章标签更多
ThinkPHP (254)
Mysql (58)
DedeCms (33)
jQuery (67)
证件照 (1)
setInc (4)
setDec (4)
onclick (5)

tp6自定义标签taglib

1,config\view.php

// 预加载自定义模板标签
    'taglib_pre_load'     =>    'app\common\taglib\Zht',
from clipboard

2,app\common\taglib\Zht.php

<?php
namespace app\common\taglib;

use think\template\TagLib;

/**
 * 自定义标签
 */
class Zht extends TagLib{
	/**
	 * 定义标签列表
	 */
	protected $tags   =  [
		'table' => ['table', 'where', 'limit', 'order', 'field', 'key', 'id'],
		'ad' => ['attr' => 'code,limit,key,id,cache'],
		'article' => ['attr' => 'cat,limit,key,id,cache'],		
	];
	/**
	 *  单表查询操作标签   
	 *  table:表名
	 *   where:查询条件
	 *   limit:limit
	 *   order:排序条件
	 *   field:需要取哪些字段
	 *   key:序号
	 *   id:循环中定义的元素变量
	 * {zht:table table="goods" field="goodsId,goodsName" limit='6'}{/zht:table}
	 */
	public function tagTable($tag, $content){
		$table   = $tag['table'];
		$where   = isset($tag['where']) ? $tag['where'] : '0';
		$order   = isset($tag['order']) ? $tag['order'] : '0';
		$field   = isset($tag['field']) ? $tag['field'] : '*';
		$limit    = isset($tag['limit']) ? (int)$tag['limit'] : 0;
		$cache  = isset($tag['cache']) ? $tag['cache'] : 0;
		$key    = isset($tag['key']) ? $tag['key'] : 'key';
		$id     = isset($tag['id']) ? $tag['id'] : 'vo';
		$parse  = '<?php ';
		$parse .= '$zhtTagTable =  \app\common\model\Tags::zhtDb("' . $table . '","' . $where . '","' . $order . '","' . $field . '",' . $limit . ',' . $cache . '); ';
		$parse .= 'foreach($zhtTagTable as $' . $key . '=>$' . $id . '){';
		$parse .= '?>';
		$parse .= $content;
		$parse .= '<?php } ?>';
		return $parse;
	}
	/**
	 * 广告数据调用
	 *  catId:广告位置id
	 *   limit:获取记录数量
	 * cache:缓存时间
	 *   key:序号
	 *    id:循环中定义的元素变量
	 * {zht:ad catId='1' limit='6'}{/zht:ad} 
	 */
	public function tagAd($tag, $content){
		$catId  = $tag['catId'];
		$order  = isset($tag['order']) ? $tag['order'] : '0';
		$limit  = isset($tag['limit'])?(int)$tag['limit']:99;
		$cache  = isset($tag['cache'])?$tag['cache']:0;
		$key    = isset($tag['key'])?$tag['key']:'key';
		$id     = isset($tag['id'])?$tag['id']:'vo';
		$parse  = '<?php ';
		$parse .= '$zhtTagAd =  \app\common\model\Tags::listAd('.$catId.','.$limit.',"' . $order . '",' . $cache.'); ';
		$parse .= 'foreach($zhtTagAd as $'.$key.'=>$'.$id.'){';
		$parse .= '?>';
		$parse .= $content;
		$parse .= '<?php } ?>';
		return $parse;
	}
	/**
	 * 文章数据调用-产业链资讯
	 *   catId:文章分类ID 或者 'new'
	 *   limit:获取记录数量
	 * cache:缓存时间
	 *   key:序号
	 *    id:循环中定义的元素变量
	 * {zht:article catId='1' limit='6'}{/zht:article} 
	 */
	public function tagArticle($tag, $content){
		$type   = isset($tag['type'])?$tag['type']:'';
		$catId  = isset($tag['catId'])?$tag['catId']:0;
		$catId   = $this->autoBuildVar($catId);
		$limit  = isset($tag['limit'])?(int)$tag['limit']:99;
		$cache  = isset($tag['cache'])?$tag['cache']:0;
		$key    = isset($tag['key'])?$tag['key']:'key';
		$id     = isset($tag['id'])?$tag['id']:'vo';
		$parse  = '<?php ';
		$parse .= '$zhtTagArticle =  \app\common\model\Tags::listArticle("'.$type.'",'.$catId.','.$limit.','.$cache.'); ';
		$parse .= 'foreach($zhtTagArticle as $'.$key.'=>$'.$id.'){';
		$parse .= '?>';
		$parse .= $content;
		$parse .= '<?php } ?>';
		return $parse;
	}
}
3,app\common\model\Tags.php

<?php
namespace app\common\model;

use think\facade\Db;

/**
 * 自定义标签模型
 */
class Tags extends Base{
	/**
	 * 单数据库查询
	 */
	public static function zhtDb($table, $where, $order, $field, $limit, $cache)	{
		$cacheData = cache('TAG_TABLE_' . $table . "_" . $field . "_" . $limit . "_" . $cache);
		if ($cacheData) return $cacheData;
		$rs = Db::name($table)->field($field)
			->where($where)
			->order($order)
			->limit($limit)
			->select();
		cache('TAG_TABLE_' . $table . "_" . $field . "_" . $limit . "_" . $cache, $rs, $cache);
		return $rs;
	}
	/**
	 * 获取广告位置
	 */
	public static function listAd($catId, $limit, $order, $cache = 0){
		$cacheData = cache('TAG_AD' . $catId . "_" . $cache);
		if ($cacheData) return $cacheData;
		$today = date('Y-m-d');
		$rs = Db::name("ad")
			->where("delete_time is null and catId='" . $catId . "' and ((startDate<= '$today' and endDate>='$today') or (startDate<= '$today' and endDate is null) or (startDate is null and endDate>='$today') or (startDate is null and endDate is null))")
			->field('adId,adTitle,adUrl,adImg')
			->order($order)
			->limit($limit)
			->select();
		cache('TAG_AD' . $catId . "_" . $cache, $rs, $cache);
		return $rs;
	}
	/**
	 * 获取文章列表
	 */
	public static function listArticle($type = '',$catId = 0, $limit, $cache = 0)	{
		$cacheData = cache('TAG_ARTICLE_' . $type . "_" . $catId . "_" . $limit . "_" . $cache);
		if ($cacheData) return $cacheData;
		$where = [];
		$where[] = ['delete_time', 'null',''];
		$where[] = ['isShow', '=', 1];
		if($catId > 0)$where[] = ['catId', '=', $catId];
		if($type != ''){
			$order = $type.' desc,dataSort desc,articleId desc';
		}else{
			$order = 'dataSort desc,articleId desc';
		}
		$rs = Db::name('article')->where($where)
			->field("articleId, articleTitle, articleImg,create_time")
			->order($order)
			->limit($limit)
			->select();
		// echo '垃'.$catId.'圾'.$goodscat;
		// die;
		cache('TAG_ARTICLE_' . $type . "_" . $catId . "_" . $limit . "_" . $cache, $rs, $cache);
		return $rs;
	}
}
4,模板中使用:

{zht:table table="about_cat" field="catId,catTitle" where='delete_time is null' limit='4' order='catId desc'}
  <li><a href="/about-lists/{$vo.catId}.html">{$vo.catTitle}</a></li>
  {/zht:table}
  <hr>
  {zht:ad catId='1' field="adId,adTitle" limit='5' order='adId desc'}
  <li><a href="/about-lists/{$vo.adId}.html">{$vo.adTitle}</a></li>
  {/zht:ad}
  <hr>
  {zht:article type='isHome' catId="1" field="articleId,articleTitle" limit='4' order='articleId desc'}
  <li><a href="/about-lists/{$vo.articleId}.html">{$vo.articleTitle}</a></li>
  {/zht:article}
  <hr>
  {php}$catId = 2;{/php}
  {zht:article type='isHome' catId="$catId" field="articleId,articleTitle" limit='4' order='articleId desc'}
  <li><a href="/about-lists/{$vo.articleId}.html">{$vo.articleTitle}</a></li>
  {/zht:article}

版权声明:本站原创文章,允许自由转载。

相关推荐
tp6控制器不存在的解决方法:控制器不存在:app\controller\Index
ThinkPHP6 | 2021-12-01 6955
当我们下载tp6,应用 多应用模式的时候,提示错误:控制器不存在:app\controller\Index,如图: 原因 多应用没有配置 解决方法 步骤如下: (1)需要安装多应用模式扩展think-multi-app 进入项目根目录。我的路径是(切记改为自己的项目路径):D:\phpStudy\PHPTutorial\...
TP6模型事件-查询后onAfterRead不好用
ThinkPHP6 | 2022-06-15 5644
TP6模型事件-查询后onAfterRead不好用 比如,我想实现浏览新闻后,浏览次数加1 实现: public static function onAfterRead($news) { $news->read_number += 1; $news->save(); } 运行后,浏览次数加1了,很好,对不对...
评论:0条
评论加载中...
发表评论