<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 知識點 1、編程思路 2、工作準備 3、菜單改寫 4、首頁最新文章 5、熱門文章 6、本站置頂 7、修改文章列表頁 8、操作動態調用模型模板 [TOC] ## 一、編程思路 一個CMS,有多個模型,如文章、圖片、視頻等,模型是后臺添加的 如果要設置自定義標簽,按之前操作,就會新建多個文件,如Article.php、Photo.php、Video.php等 這樣很麻煩,中間還要修改字段、數據表等等信息,不便于拓展 所以,我們只新建一個taglib文件,如Def.php 其中的數據表、字段、數量等信息,以變量形式出現 公共標簽,獲取不同模型的數據 這樣,自定義標簽,就不會受不同模型、不同數據表、不同字段等因素影響 便于拓展,使用也方面,文件也簡單 ## 二、工作準備 ### 1、復制Article.php 位置:\application\index\taglib\Article.php 改名:Def.php 修改相應代碼 ### 2、修改配置文件 修改標簽預加載 位置:\application\config.php 代碼: ~~~ 'template' => [ 'taglib_pre_load' => 'app\index\taglib\Article', ], 改成 'template' => [ 'taglib_pre_load' => 'app\index\taglib\Def', ], ~~~ ## 三、菜單改寫 所有頁面都要調用分類菜單,所以修改比較簡單,只需修改模板中調用方式 標簽定義、標簽申明都不用修改 ~~~ <ul class="down-menu nav-menu"> <li class="current-menu-item"><a rel="nofollow" href="/"><i class="fa fa-home"></i> 首頁</a></li> {def: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> {/def:menu} </ul> ~~~ ## 四、首頁最新文章 ### (一)標簽定義 增加兩個屬性table(數據表)、field(表字段),方便多模型調用 ~~~ protected $tags = [ 'menu' => ['attr' => 'name', 'close' => 1], //0閉合標簽 ,1不閉合(默認) 'hot' => ['attr' => 'catid,num,name,table,field', 'close' => 1], 'recommend' => ['attr' => 'catid,num,name,table,field', 'close' => 1], 'list' => ['attr' => 'catid,num,name,table,field', 'close' => 1], 'page' => ['attr' => 'catid', 'close' => 1], //實際不使用,僅供教學參考 ]; ~~~ ### (二)標簽申明 ~~~ public function tagList($tag, $content) { $table = $tag['table']; $field = $tag['field']; //首頁cid=0,num>0 ; 分類頁cid>0,num=0 if(empty($tag['catid']) || $tag['catid']=='0'){ $num = $tag['num']; $php = <<<php <?php \$list = think\Db::name('$table')->field('$field')->limit("$num")->order('listorder desc')->cache('$table_index',config('cache.expire'))->select(); \$__LIST__ = \$list; ?> php; }else{ $num = 0; $where = "catid=" . $tag['catid']; $php = <<<php <?php \$list = think\Db::name('$table')->where("$where")->field('$field')->limit("$num")->order('listorder desc')->paginate(); \$__LIST__ = \$list; ?> php; } $name = $tag['name']; $parse = $php; $parse .= '{volist name="__LIST__" id="' . $name . '"}'; $parse .= $content; $parse .= '{/volist}'; return $parse; } ~~~ ### (三)標簽調用 ~~~ {def:list name="vo" catid="0" num="2" table="article" field="id,catid,title,thumb,description,inputtime,views"} <article class="post type-post status-publish format-standard hentry"> <figure class="thumbnail"> <a href="{:url('info',['id'=>$vo.id])}"> <img src="__UPLOADS__/{$vo.thumb}" alt="{$vo.title}"></a> <span class="cat"> <a href="{:url('category',['id'=>$vo.catid])}">{$vo.catid|getCatInfoById=catname}</a></span> </figure> <header class="entry-header"> <h2 class="entry-title"><a href="{:url('info',['id'=>$vo.id])}" 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="{:url('info',['id'=>$vo.id])}"> <i class="fa fa-comment-o"></i> 查看評論</a></span> </span> <div class="clear"></div> </div> <span class="entry-more"> <a href="{:url('info',['id'=>$vo.id])}" rel="bookmark">閱讀全文</a></span> </article> {/def:list} ~~~ ## 五、熱門文章 ### (一)標簽定義 增加兩個屬性table(數據表)、field(表字段),方便多模型調用 ~~~ protected $tags = [ 'menu' => ['attr' => 'name', 'close' => 1], //0閉合標簽 ,1不閉合(默認) 'hot' => ['attr' => 'catid,num,name,table,field', 'close' => 1], 'recommend' => ['attr' => 'catid,num,name,table,field', 'close' => 1], 'list' => ['attr' => 'catid,num,name,table,field', 'close' => 1], 'page' => ['attr' => 'catid', 'close' => 1], //實際不使用,僅供教學參考 ]; ~~~ ### (二)標簽申明 ~~~ public function tagHot($tag, $content) { $table = $tag['table']; $field = $tag['field']; //可為空,則查詢所有字段 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 \$hot = think\Db::name('$table')->where("$where")->field('$field')->limit('$num')->order('views desc')->cache("$cache",config('cache.expire'))->select(); \$__LIST__ = \$hot; ?> php; $name = $tag['name']; $parse = $php; $parse .= '{volist name="__LIST__" id="' . $name . '"}'; $parse .= $content; $parse .= '{/volist}'; return $parse; } ~~~ ### (三)標簽調用 ~~~ <aside class="widget widget_hot_post"> <h3 class="widget-title"><i class="fa-bars fa"></i>熱門文章</h3> <div id="hot_post_widget"> <ul> {def:hot name="vo" catid="0" num="8" table="article" field="id,title"} <li><span class="li-icon li-icon-{$key+1}">{$key+1}</span><a href="{:url('info',['id'=>$vo.id])}">{$vo.title}</a></li> {/def:hot} </ul> </div> </aside> ~~~ ## 六、本站置頂 ### (一)標簽定義 增加兩個屬性table(數據表)、field(表字段),方便多模型調用 ~~~ protected $tags = [ 'menu' => ['attr' => 'name', 'close' => 1], //0閉合標簽 ,1不閉合(默認) 'hot' => ['attr' => 'catid,num,name,table,field', 'close' => 1], 'recommend' => ['attr' => 'catid,num,name,table,field', 'close' => 1], 'list' => ['attr' => 'catid,num,name,table,field', 'close' => 1], 'page' => ['attr' => 'catid', 'close' => 1], //實際不使用,僅供教學參考 ]; ~~~ ### (二)標簽申明 ~~~ public function tagRecommend($tag, $content) { $table = $tag['table']; $field = $tag['field']; 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']; } $cache = 'recommend_' . $tag['catid']; $php = <<<php <?php \$recommend = think\Db::name('$table')->where("$where")->field('$field')->limit('$num')->order('listorder desc')->cache("$cache",config('cache.expire'))->select(); \$__LIST__ = \$recommend; ?> php; $name = $tag['name']; $parse = $php; $parse .= '{volist name="__LIST__" id="' . $name . '"}'; $parse .= $content; $parse .= '{/volist}'; return $parse; } ~~~ ### (三)標簽調用 ~~~ {block name="sidebar"} <aside class="widget widget_hot_commend"> <h3 class="widget-title"><i class="fa-bars fa"></i>本站推薦</h3> <div id="hot" class="hot_commend"> <ul> {def:recommend name="vo" catid="0" num="3" table="article" field="id,title,thumb,views"} <li> <figure class="thumbnail"><a href="{:url('info',['id'=>$vo.id])}"><img src="__UPLOADS__/{$vo.thumb}" alt="{$vo.title}"></a></figure> <div class="hot-title"><a href="{:url('info',['id'=>$vo.id])}">{$vo.title}</a></div> <div class="views">閱讀 {$vo.views}</div> <i class="fa-thumbs-o-up fa"> 0</i> </li> {/def:recommend} </ul> <div class="clear"></div> </div> </aside> ~~~ ## 六、本站置頂 ### (一)標簽定義 增加兩個屬性table(數據表)、field(表字段),方便多模型調用 ~~~ protected $tags = [ 'menu' => ['attr' => 'name', 'close' => 1], //0閉合標簽 ,1不閉合(默認) 'hot' => ['attr' => 'catid,num,name,table,field', 'close' => 1], 'recommend' => ['attr' => 'catid,num,name,table,field', 'close' => 1], 'list' => ['attr' => 'catid,num,name,table,field', 'close' => 1], 'page' => ['attr' => 'catid', 'close' => 1], //實際不使用,僅供教學參考 ]; ~~~ ### (二)標簽申明 ~~~ public function tagRecommend($tag, $content) { $table = $tag['table']; $field = $tag['field']; 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']; } $cache = 'recommend_' . $tag['catid']; $php = <<<php <?php \$recommend = think\Db::name('$table')->where("$where")->field('$field')->limit('$num')->order('listorder desc')->cache("$cache",config('cache.expire'))->select(); \$__LIST__ = \$recommend; ?> php; $name = $tag['name']; $parse = $php; $parse .= '{volist name="__LIST__" id="' . $name . '"}'; $parse .= $content; $parse .= '{/volist}'; return $parse; } ~~~ ### (三)標簽調用 ~~~ <aside class="widget widget_hot_commend"> <h3 class="widget-title"><i class="fa-bars fa"></i>本站推薦</h3> <div id="hot" class="hot_commend"> <ul> {def:recommend name="vo" catid="0" num="3" table="article" field="id,title,thumb,views"} <li> <figure class="thumbnail"><a href="{:url('info',['id'=>$vo.id])}"><img src="__UPLOADS__/{$vo.thumb}" alt="{$vo.title}"></a></figure> <div class="hot-title"><a href="{:url('info',['id'=>$vo.id])}">{$vo.title}</a></div> <div class="views">閱讀 {$vo.views}</div> <i class="fa-thumbs-o-up fa"> 0</i> </li> {/def:recommend} </ul> <div class="clear"></div> </div> </aside> ~~~ ## 七、修改文章列表頁 主要修改模板代碼 ~~~ {block name="main"} {def:list name="vo" catid="$id" num="0" table="article" field="id,catid,title,thumb,description,inputtime,views"} <article class="post type-post status-publish format-standard hentry"> <figure class="thumbnail"> <a href="{:url('info',['id'=>$vo.id])}"> <img src="__UPLOADS__/{$vo.thumb}" alt="{$vo.title}"></a> <span class="cat"> <a href="{:url('category',['id'=>$vo.catid])}">{$vo.catid|getCatInfoById=catname}</a></span> </figure> <header class="entry-header"> <h2 class="entry-title"><a href="{:url('info',['id'=>$vo.id])}" 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="{:url('info',['id'=>$vo.id])}"> <i class="fa fa-comment-o"></i> 查看評論</a></span> </span> <div class="clear"></div> </div> <span class="entry-more"> <a href="{:url('info',['id'=>$vo.id])}" rel="bookmark">閱讀全文</a></span> </article> <div class="layui-box layui-laypage layui-laypage-molv" style="float:right;"> {$__LIST__->render()} </div> {/def:list} {/block} ~~~ ## 八、操作動態調用模型模板 思路:不同模型的文章,調用不同的模板,如:文章就調用文章模型的模板、圖片就調用圖片模型的模板、視頻就調用視頻模型的模板 實現:鏈接處傳參,兩個,一個是文章ID、一個是分類ID,根據catid查詢模型,然后根據模型調用對應的模板 ### 1、添加鏈接 傳參2個:一個是文章ID、一個是分類ID 文章ID:用于查詢具體信息 分類ID:用于查詢模型,調用對應的模板 ~~~ <a href="{:url('info',['id'=>$vo.id,'catid'=>$vo.catid])}" rel="bookmark">{$vo.title}</a> ~~~ ### 2、修改操作 列表頁操作 ~~~ public function category(){ $id = input('id'); //分類ID $this->assign('id',$id); $template = getModInfoById($id,'category_template'); return view('../application/index/view/default/index/' . $template); } ~~~ 內容頁操作 ~~~ public function info(){ $id = input('id',0);//ID $catid = input('catid',0);//分類ID $modelInfo = getModInfoById($catid); $info = Db::name($modelInfo['tablename'])->where('id',$id)->find(); $catinfo = getCatInfoById($catid); $p_catname = getCatInfoById($catinfo['parentid'],'catname'); $this->assign('info',$info); $this->assign('catid',$catid); $this->assign('catname',$catinfo['catname']); $this->assign('p_catname',$p_catname); return view('../application/index/view/default/index/' . $modelInfo['show_template']); } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看