ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 知识点: 1、改进思路 2、改菜单(非闭合标签) 3、改右侧本站推荐 4、自定义标签缓存设计 5、改进热门文章 6、首页最新文章 7、分类页文章列表 [TOC] ## 一、改进思路 自定义标签:就是整合PHP、html代码,方便调用 重新布局模板,不同模板可以随意切换,自定义标签也需要更换。 自定义标签仅仅取的是数据,模板代码不放在标签里,这样可以适合不同的模板。 那么这个,就要用非闭合标签了 之前用闭合标签,适合模板只有一套,调用比较简单;如果有多套模板就,闭合标签就行不通了 两种方法都要掌握,具体要看用在什么地方和什么时候 比如单页制作的时候,就可以用闭合标签 ## 二、改菜单(非闭合标签) ### 1、删除Common.php 将初始化代码放到自定义标签中去 位置:\application\index\controller\Common.php index.php继承Controller build.php去掉common控制器 ### 2、菜单标签定义 属性name ~~~ protected $tags = [ 'menu' => ['attr' => 'name', 'close' => 1], //0闭合标签 ,1不闭合(默认) 'hot' => ['attr' => 'catid,num', 'close' => 0], 'recommend' => ['attr' => 'catid,num', 'close' => 0], 'list' => ['attr' => 'catid,num', 'close' => 0], ]; ~~~ ### 3、菜单标签申明 两个参数:$tag(标签定义属性)、$content(标签解析内容) ~~~ public function tagMenu($tag, $content) { $php = <<<php <?php if(!cache('menu')) { \$categoryArray = think\Db::name('category')->order('listorder')->select(); cache('menu',app\admin\model\Menu::tree(\$categoryArray)); } \$__LIST__ = cache('menu'); ?> php; $name = $tag['name']; $parse = $php; $parse .= '{volist name"__LIST__" id="' . $name . '"}'; $parse .= $content; $parse .= '{/volist}'; return $parse; } ~~~ 注意:转义字符运用和缓存机制运用 ### 4、缓存机制 修改配置文件 \application\config.php ~~~ // +---------------------------------------------------------------------- // | 缓存设置 // +---------------------------------------------------------------------- 'cache' => [ // 驱动方式 'type' => 'File', // 缓存保存目录 'path' => CACHE_PATH, // 缓存前缀 'prefix' => '', // 缓存有效期 0表示永久缓存 'expire' => 3600, // 3600表示一个小时 ], if(!cache('menu')) { \$categoryArray = think\Db::name('category')->order('listorder')->select(); cache('menu',app\admin\model\Menu::tree(\$categoryArray)); \$__LIST__ = cache('menu'); }else{ \$__LIST__ = cache('menu'); } ~~~ 简化成 ~~~ if(!cache('menu')) { \$categoryArray = think\Db::name('category')->order('listorder')->select(); cache('menu',app\admin\model\Menu::tree(\$categoryArray)); } \$__LIST__ = cache('menu'); ~~~ 少写一行数据 ### 5、模板调用自定义标签 格式:{article:menu name="vo"}……{/article:menu} ~~~ <ul class="down-menu nav-menu"> <li class="current-menu-item"><a rel="nofollow" href="/"><i class="fa fa-home"></i> 首页</a></li> {article:menu name="vo"} <li> <a href="javascript:(0)"> {$vo.catname}</a> <ul class="sub-menu"> {volist name="vo.son" id="voson"} <li><a href="{:url('category',['id'=>$voson.id])}"> {$voson.catname}</a></li> {/volist} </ul> </li> {/article:menu} </ul> ~~~ ## 三、改右侧本站推荐 ### (一)标签定义 ~~~ protected $tags = [ 'menu' => ['attr' => 'name', 'close' => 1], //0闭合标签 ,1不闭合(默认) 'hot' => ['attr' => 'catid,num', 'close' => 0], 'recommend' => ['attr' => 'catid,num,name', 'close' => 1], 'list' => ['attr' => 'catid,num', 'close' => 0], ]; ~~~ ### (二)标签申明 ~~~ public function tagRecommend($tag, $content) { if(empty($tag['catid']) || $tag['catid']=='0'){ $where = 'recommend=1'; }else{ $where = "recommend=1 and catid=" . $tag['catid']; } if(empty($tag['num']) || $tag['num']=='0'){ $num = 3; }else{ $num = $tag['num']; } $php = <<<php <?php \$article_recommend = think\Db::name('article')->where("$where")->field('id,title,thumb,views')->limit('$num')->order('listorder desc')->select(); \$__LIST__ = \$article_recommend; ?> php; $name = $tag['name']; $parse = $php; $parse .= '{volist name="__LIST__" id="' . $name . '"}'; $parse .= $content; $parse .= '{/volist}'; return $parse; } ~~~ 原标签申明 标签解析:本站推荐512 ~~~ public function tagRecommend512($tag) { if(empty($tag['catid']) || $tag['catid']=='0'){ $where = 'recommend=1'; }else{ $where = "recommend=1 and catid=" . $tag['catid']; } if(empty($tag['num']) || $tag['num']=='0'){ $num = 3; }else{ $num = $tag['num']; } $php = <<<php <?php \$article_recommend = think\Db::name('article')->where("$where")->field('id,title,thumb,views')->limit('$num')->order('listorder desc')->select(); \$__LIST__ = \$article_recommend; ?> php; $parse = '<aside class="widget widget_hot_commend">'; $parse .= '<h3 class="widget-title"><i class="fa-bars fa"></i>本站推荐</h3>'; $parse .= '<div id="hot" class="hot_commend">'; $parse .= '<ul>'; $parse .= $php; $parse .= '{volist name="__LIST__" id="vo"}'; $parse .= '<li>'; $parse .= '<figure class="thumbnail"><a href="#"><img src="__UPLOADS__/image/{$vo.thumb}" alt="{$vo.title}"></a></figure>'; $parse .= '<div class="hot-title"><a href="#">{$vo.title}</a></div>'; $parse .= '<div class="views">阅读 {$vo.views}</div>'; $parse .= '<i class="fa-thumbs-o-up fa"> 0</i>'; $parse .= '</li>'; $parse .= '{/volist}'; $parse .= '</ul>'; $parse .= '<div class="clear"></div>'; $parse .= '</div>'; $parse .= '</aside>'; return $parse; } ~~~ 原标签调用:{Article:tagRecommend512 catid="$id" num="10" /} ### (三)标签调用 ~~~ <aside class="widget widget_hot_commend"> <h3 class="widget-title"><i class="fa-bars fa"></i>本站推荐</h3> <div id="hot" class="hot_commend"> <ul> {article:recommend name="vo" catid="0" num="3" /} <li> <figure class="thumbnail"><a href="#"><img src="__UPLOADS__/{$vo.thumb}" alt="{$vo.title}"></a></figure> <div class="hot-title"><a href="#">{$vo.title}</a></div> <div class="views">阅读 {$vo.views}</div> <i class="fa-thumbs-o-up fa"> 0</i> </li> {/article:recommend} </ul> <div class="clear"></div> </div> </aside> ~~~ ## 四、自定义标签缓存设计 ### (一)判断添加缓存 ~~~ if(!cache('menu')) { \$categoryArray = think\Db::name('category')->order('listorder')->select(); cache('menu',app\admin\model\Menu::tree(\$categoryArray)); \$__LIST__ = cache('menu'); }else{ \$__LIST__ = cache('menu'); } ~~~ 简化成 ~~~ if(!cache('menu')) { \$categoryArray = think\Db::name('category')->order('listorder')->select(); cache('menu',app\admin\model\Menu::tree(\$categoryArray)); } \$__LIST__ = cache('menu'); ~~~ 少写一行数据 ### (二)TP5自带缓存方法 自动识别首页和分类页的缓存数据,每个分页标识不能一样。比如:首页分类ID为0,其它分类ID大于0 ~~~ $cache = 'recommend_' . $tag['catid']; cache("$cache",config('cache.expire')) ~~~ 定界符内容 ~~~ $php = <<<php <?php \$article_recommend = think\Db::name('article') ->where("$where") ->field('id,title,thumb,views') ->limit('$num') ->order('listorder desc') ->cache("$cache",config('cache.expire')) ->select(); \$__LIST__ = \$article_recommend; ?> php; ~~~ 模板中务必传入catid,否则缓存一样的 ~~~ <aside class="widget widget_hot_commend"> <h3 class="widget-title"><i class="fa-bars fa"></i>本站推荐</h3> <div id="hot" class="hot_commend"> <ul> {article:recommend name="vo" catid="$id" num="6" /} <li> <figure class="thumbnail"><a href="#"><img src="__UPLOADS__/image/{$vo.thumb}" alt="{$vo.title}"></a></figure> <div class="hot-title"><a href="#">{$vo.title}</a></div> <div class="views">阅读 {$vo.views}</div> <i class="fa-thumbs-o-up fa"> 0</i> </li> {/article:recommend} </ul> <div class="clear"></div> </div> </aside> ~~~ ## 五、改进热门文章 ### (一)标签定义 ~~~ protected $tags = [ 'menu' => ['attr' => 'name', 'close' => 1], //0闭合标签 ,1不闭合(默认) 'hot' => ['attr' => 'catid,num,name', 'close' => 1], 'recommend' => ['attr' => 'catid,num,name', 'close' => 1], 'list' => ['attr' => 'catid,num,name', 'close' => 1], ]; ~~~ ### (二)标签申明 ~~~ public function tagHot($tag, $content) { if(empty($tag['catid']) || $tag['catid']=='0'){ $where = ''; }else{ $where = "catid=" . $tag['catid']; } if(empty($tag['num']) || $tag['num']=='0'){ $num = 8; }else{ $num = $tag['num']; } $cache = 'hot_' . $tag['catid']; $php = <<<php <?php \$article_hot = think\Db::name('article')->where("$where")->field('id,title')->limit('$num')->order('views desc')->cache("$cache",config('cache.expire'))->select(); \$__LIST__ = \$article_hot; ?> php; $name = $tag['name']; $parse = $php; $parse .= '{volist name="__LIST__" id="' . $name . '"}'; $parse .= $content; $parse .= '{/volist}'; return $parse; } ~~~ ### (三)缓存运用 ~~~ $cache = 'hot_' . $tag['catid']; \$article_hot = think\Db::name('article') ->where("$where") ->field('id,title') ->limit('$num') ->order('views desc') ->cache("$cache",config('cache.expire')) ->select(); ~~~ ### (四)模板调用 ~~~ <aside class="widget widget_hot_post"> <h3 class="widget-title"><i class="fa-bars fa"></i>热门文章</h3> <div id="hot_post_widget"> <ul> {article:hot name="vo" catid="$id" num="10"} <li><span class="li-icon li-icon-{$key+1}">{$key+1}</span><a href="#">{$vo.title}</a></li> {/article:hot} </ul> </div> </aside> ~~~ ## 六、首页最新文章 ### (一)标签定义 ~~~ protected $tags = [ 'menu' => ['attr' => 'name', 'close' => 1], //0闭合标签 ,1不闭合(默认) 'hot' => ['attr' => 'catid,num,name', 'close' => 1], 'recommend' => ['attr' => 'catid,num,name', 'close' => 1], 'list' => ['attr' => 'catid,num,name', 'close' => 1], ]; ~~~ ### (二)标签申明 ~~~ public function tagList($tag, $content) { //首页cid=0,num>0 ; 分类页cid>0,num=0 if(empty($tag['catid']) || $tag['catid']=='0'){ $num = $tag['num']; $php = <<<php <?php \$article_list = think\Db::name('article')->field('id,catid,title,thumb,description,inputtime,views')->limit("$num")->order('listorder desc')->cache('article_index',config('cache.expire'))->select(); \$__LIST__ = \$article_list; ?> php; }else{ $num = 0; $where = "catid=" . $tag['catid']; $php = <<<php <?php \$article_list = think\Db::name('article')->where("$where")->field('id,catid,title,thumb,description,inputtime,views')->limit("$num")->order('listorder desc')->paginate(); \$__LIST__ = \$article_list; ?> php; } $name = $tag['name']; $parse = $php; $parse .= '{volist name="__LIST__" id="' . $name . '"}'; $parse .= $content; $parse .= '{/volist}'; return $parse; } ~~~ ### (三)标签调用 ~~~ {article:list name="vo" catid="0" num="2"} <article class="post type-post status-publish format-standard hentry"> <figure class="thumbnail"> <a href="#"> <img src="__UPLOADS__/{$vo.thumb}" alt="{$vo.title}"></a> <span class="cat"> <a href="#">{$vo.catid|getCatInfoById=catname}</a></span> </figure> <header class="entry-header"> <h2 class="entry-title"><a href="#" rel="bookmark">{$vo.title}</a></h2> </header> <div class="entry-content"> <div class="archive-content">{$vo.description}</div> <br/> <span class="title-l"></span> <span class="entry-meta"> <span class="date">{$vo.inputtime}&nbsp;</span> <span class="views"> 阅读 {$vo.views}&nbsp;</span> <span class="comment"><a href="#"> <i class="fa fa-comment-o"></i> 查看评论</a></span> </span> <div class="clear"></div> </div> <span class="entry-more"> <a href="#" rel="bookmark">阅读全文</a></span> </article> {/article:list} ~~~ ## 七、分类页文章列表 ### 知识点: 分类页文章列表,num为0,因为控制数量是由控制器完成的;但首页文章列表的num是有数字限制的 首页cid=0,num>0 ; 分类页cid>0,num=0 ### (一)标签定义 ~~~ protected $tags = [ 'menu' => ['attr' => 'name', 'close' => 1], //0闭合标签 ,1不闭合(默认) 'hot' => ['attr' => 'catid,num,name', 'close' => 1], 'recommend' => ['attr' => 'catid,num,name', 'close' => 1], 'list' => ['attr' => 'catid,num,name', 'close' => 1], ]; ~~~ ### (二)标签申明 ~~~ public function tagList($tag, $content) { //首页cid=0,num>0 ; 分类页cid>0,num=0 if(empty($tag['catid']) || $tag['catid']=='0'){ $num = $tag['num']; $php = <<<php <?php \$article_list = think\Db::name('article')->field('id,catid,title,thumb,description,inputtime,views')->limit("$num")->order('listorder desc')->cache('article_index',config('cache.expire'))->select(); \$__LIST__ = \$article_list; ?> php; }else{ $num = 0; $where = "catid=" . $tag['catid']; $php = <<<php <?php \$article_list = think\Db::name('article')->where("$where")->field('id,catid,title,thumb,description,inputtime,views')->limit("$num")->order('listorder desc')->paginate(); \$__LIST__ = \$article_list; ?> php; } $name = $tag['name']; $parse = $php; $parse .= '{volist name="__LIST__" id="' . $name . '"}'; $parse .= $content; $parse .= '{/volist}'; return $parse; } ~~~ ### (三)标签调用 ~~~ {article:list name="vo" catid="$id" num="0"} <article class="post type-post status-publish format-standard hentry"> <figure class="thumbnail"> <a href="#"> <img src="__UPLOADS__/{$vo.thumb}" alt="{$vo.title}"></a> <span class="cat"> <a href="#">{$vo.catid|getCatInfoById=catname}</a></span> </figure> <header class="entry-header"> <h2 class="entry-title"><a href="#" rel="bookmark">{$vo.title}</a></h2> </header> <div class="entry-content"> <div class="archive-content">{$vo.description}</div> <br/> <span class="title-l"></span> <span class="entry-meta"> <span class="date">{$vo.inputtime}&nbsp;</span> <span class="views"> 阅读 {$vo.views}&nbsp;</span> <span class="comment"><a href="#"> <i class="fa fa-comment-o"></i> 查看评论</a></span> </span> <div class="clear"></div> </div> <span class="entry-more"> <a href="#" rel="bookmark">阅读全文</a></span> </article> <div class="layui-box layui-laypage layui-laypage-molv" style="float:right;"> {$__LIST__->render()} </div> {/article:list} ~~~ ### (四)文章分页 #### 方法一(推荐) 1、标签申明 ~~~ public function tagList($tag, $content) { //首页cid=0,num>0 ; 分类页cid>0,num=0 if(empty($tag['catid']) || $tag['catid']=='0'){ $num = $tag['num']; $php = <<<php <?php \$article_list = think\Db::name('article')->field('id,catid,title,thumb,description,inputtime,views')->limit("$num")->order('listorder desc')->cache('article_index',config('cache.expire'))->select(); \$__LIST__ = \$article_list; ?> php; }else{ $num = 0; $where = "catid=" . $tag['catid']; $php = <<<php <?php \$article_list = think\Db::name('article')->where("$where")->field('id,catid,title,thumb,description,inputtime,views')->limit("$num")->order('listorder desc')->paginate(); \$__LIST__ = \$article_list; ?> php; } $name = $tag['name']; $parse = $php; $parse .= '{volist name="__LIST__" id="' . $name . '"}'; $parse .= $content; $parse .= '{/volist}'; return $parse; } ~~~ 2、模板调用 ~~~ {article:list name="vo" catid="$id" num="0"} <article class="post type-post status-publish format-standard hentry"> <figure class="thumbnail"> <a href="#"> <img src="__UPLOADS__/{$vo.thumb}" alt="{$vo.title}"></a> <span class="cat"> <a href="#">{$vo.catid|getCatInfoById=catname}</a></span> </figure> <header class="entry-header"> <h2 class="entry-title"><a href="#" rel="bookmark">{$vo.title}</a></h2> </header> <div class="entry-content"> <div class="archive-content">{$vo.description}</div> <br/> <span class="title-l"></span> <span class="entry-meta"> <span class="date">{$vo.inputtime}&nbsp;</span> <span class="views"> 阅读 {$vo.views}&nbsp;</span> <span class="comment"><a href="#"> <i class="fa fa-comment-o"></i> 查看评论</a></span> </span> <div class="clear"></div> </div> <span class="entry-more"> <a href="#" rel="bookmark">阅读全文</a></span> </article> <div class="layui-box layui-laypage layui-laypage-molv" style="float:right;"> {$__LIST__->render()} </div> {/article:list} ~~~ #### 方法二 1、标签申明 ~~~ public function tagList($tag, $content) { //首页cid=0,num>0 ; 分类页cid>0,num=0 if(empty($tag['catid']) || $tag['catid']=='0'){ $num = $tag['num']; $php = <<<php <?php \$article_list = think\Db::name('article')->field('id,catid,title,thumb,description,inputtime,views')->limit("$num")->order('listorder desc')->cache('article_index',config('cache.expire'))->select(); \$__LIST__ = \$article_list; ?> php; }else{ $num = 0; $where = "catid=" . $tag['catid']; $php = <<<php <?php \$article_list = think\Db::name('article')->where("$where")->field('id,catid,title,thumb,description,inputtime,views')->limit("$num")->order('listorder desc')->paginate(); \$__LIST__ = \$article_list; ?> php; } $name = $tag['name']; $parse = $php; $parse .= '{volist name="__LIST__" id="' . $name . '"}'; $parse .= $content; $parse .= '{/volist}'; if($num==0){ $parse .= '<div class="layui-box layui-laypage layui-laypage-molv" style="float:right;">'; $parse .= '{$__LIST__->render()}'; $parse .= '</div>'; } return $parse; } ~~~ 2、标签调用 ~~~ {article:list name="vo" catid="$id" num="0"} <article class="post type-post status-publish format-standard hentry"> <figure class="thumbnail"> <a href="#"> <img src="__UPLOADS__/{$vo.thumb}" alt="{$vo.title}"></a> <span class="cat"> <a href="#">{$vo.catid|getCatInfoById=catname}</a></span> </figure> <header class="entry-header"> <h2 class="entry-title"><a href="#" rel="bookmark">{$vo.title}</a></h2> </header> <div class="entry-content"> <div class="archive-content">{$vo.description}</div> <br/> <span class="title-l"></span> <span class="entry-meta"> <span class="date">{$vo.inputtime}&nbsp;</span> <span class="views"> 阅读 {$vo.views}&nbsp;</span> <span class="comment"><a href="#"> <i class="fa fa-comment-o"></i> 查看评论</a></span> </span> <div class="clear"></div> </div> <span class="entry-more"> <a href="#" rel="bookmark">阅读全文</a></span> </article> {/article:list} ~~~ #### 方法三(仅供理解) 1、标签定义 ~~~ protected $tags = [ 'menu' => ['attr' => 'name', 'close' => 1], //0闭合标签 ,1不闭合(默认) 'hot' => ['attr' => 'catid,num,name', 'close' => 1], 'recommend' => ['attr' => 'catid,num,name', 'close' => 1], 'list' => ['attr' => 'catid,num,name', 'close' => 1], 'page' => ['attr' => 'catid', 'close' => 1], //实际不使用,仅供教学参考 ]; ~~~ 2、标签申明 ~~~ public function tagPage($tag, $content) { //实际不使用,仅供教学参考 $where = "catid=" . $tag['catid']; $php = <<<php <?php \$article_list = think\Db::name('article')->where("$where")->field('id,catid,title,thumb,description,inputtime,views')->limit("$num")->order('listorder desc')->paginate(); \$vo = \$article_list; ?> php; $parse = $php; $parse .= $content; return $parse; } ~~~ 3、模板调用 ~~~ {article:list name="vo" catid="$id" num="0"} <article class="post type-post status-publish format-standard hentry"> <figure class="thumbnail"> <a href="#"> <img src="__UPLOADS__/{$vo.thumb}" alt="{$vo.title}"></a> <span class="cat"> <a href="#">{$vo.catid|getCatInfoById=catname}</a></span> </figure> <header class="entry-header"> <h2 class="entry-title"><a href="#" rel="bookmark">{$vo.title}</a></h2> </header> <div class="entry-content"> <div class="archive-content">{$vo.description}</div> <br/> <span class="title-l"></span> <span class="entry-meta"> <span class="date">{$vo.inputtime}&nbsp;</span> <span class="views"> 阅读 {$vo.views}&nbsp;</span> <span class="comment"><a href="#"> <i class="fa fa-comment-o"></i> 查看评论</a></span> </span> <div class="clear"></div> </div> <span class="entry-more"> <a href="#" rel="bookmark">阅读全文</a></span> </article> {/article:list} {article:page catid="$id"} <div class="layui-box layui-laypage layui-laypage-molv" style="float:right;"> {$vo->render()} </div> {/article:page} ~~~