亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 42
粉絲 2
評(píng)論 0
訪問量 63672
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
模版的渲染 布局 繼承 常用標(biāo)簽(模版直接輸出 模版賦值 模版的過濾與替換 模版布局 模版繼承 常用的循環(huán)標(biāo)簽 范圍取值標(biāo)簽)2019年3月22日
小明的博客
原創(chuàng)
1171人瀏覽過

一、模版輸出

  • 引入View類 使用視圖方法display() 不通過模版  直接渲染內(nèi)容  可以添加html標(biāo)簽
    return View::display($name);

       return View::display('我的姓名是:<span style="color: red">'.$name.'</span>');

  • fetch(模版表達(dá)式)
    //       模版表達(dá)式:模塊@控制器/操作方法
    //       模版默認(rèn)目錄是  模塊/view/控制器名/模版文件名(一般與操作方法同名)
    //       模型文件的后綴一般為html,可以在config/template.php中修改
    //       fetch('模版表達(dá)式', [模版變量數(shù)組], [模版配置參數(shù)數(shù)組])
    //       模版參數(shù)我們一般不再控制器里動(dòng)態(tài)設(shè)定 而是在配置文件里設(shè)置
    //       這里的模版表達(dá)式可以省略模塊和控制器
    //       return View::fetch('index@index/demo1', ['name'=>$name]);

  • 其實(shí)在controller類中有一個(gè)屬性view,保存著視圖類的實(shí)例化對(duì)象
    //       所以要讓index類繼承controller類 然后實(shí)例化他就可以模版輸出
    //       return $this->view->fetch('demo1', ['name'=>$name]);
    //       通??梢允÷詖iew這個(gè)屬性
    //       return $this->fetch('demo1', ['name'=>$name]);
    //       框架也內(nèi)置了助手函數(shù) view()  在任何情況下都可用使用他  不用引入任何類
          return view('demo1', ['name'=>$name]);

    二、模版賦值

  • 1.assign()使用他必須用view類,以后我們統(tǒng)一用cotroller類實(shí)例化調(diào)用
    //        View::assign('name', $name);
    //        return View::fetch();

  • //        2.傳參數(shù)fetch() view()剛才演示過了

  • //        3.對(duì)象方式  通過controller類中的兩個(gè)魔術(shù)方法實(shí)現(xiàn)數(shù)據(jù)注入
           $this->view->name = $name;
    //        同樣的view可以省略
    //        $this->view->assign('name', $name);
    //        如果按照默認(rèn)規(guī)則創(chuàng)建的模版文件  那么就可以省略模版文件名
           return $this->fetch();

三、模版過濾與替換

//    tp51同之前的版本直接刪除了模版字符串替換功能,改為模版配置參數(shù)來實(shí)現(xiàn)
//    congfig/tmplate.php中更改

//字符過濾
//    'tpl_replace_str' => ['ji ming'=>'冀曉明']

$this->view->assign('name', 'ji ming');
$filter = function ($content) {
   return str_replace('ji ming', '冀曉明', $content);
};

return $this->filter($filter)->fetch();

四、模版布局

//1、全局布局
//先在template.php中開啟

/*//開啟全局布局
'layout_on' => true,
//全局模版名
'layout_name' => 'layout',
//主體占位符
'layout_item' => '{__CONTENT__}'*/

//然后在view下建立public/header和footer  寫出html樣式  然后在view下新建lauout.html

{//網(wǎng)站頭部}
{include file="public/header" /}
{//網(wǎng)站主體}
{__CONTENT__}
{//網(wǎng)站尾部}
{include file="public/footer" /}

//其中{__CONTENT__}是占位符  demo4里面的html內(nèi)容重寫網(wǎng)站主題內(nèi)容

//2、模版標(biāo)簽
//不需要在配置中開啟全局布局 也不需要配置任何
//需要在布局模版的模版文件中添加標(biāo)簽{layout name="layout" /}

//3、在控制器動(dòng)態(tài)配置
       //不需要配置  不需要添加任何標(biāo)簽
       //布局模版還是要實(shí)現(xiàn)準(zhǔn)備好的
       //直接調(diào)用:$this->view->engine->layout(true);
       //需要所有都是默認(rèn)的   'layout_item'=>'{__CONTENT__}'  'layout_name'=>'layout'
//        $this->view->engine->layout(true);
//        return $this->view->fetch();
//        可以簡化成一行 但是沒有參數(shù)是錯(cuò)誤的
//        return $this->view->engine->layout(true)->fetch();
//        參數(shù)應(yīng)該是完整的模版表達(dá)式
//        return $this->view->engine->layout(true)->fetch('index@index/demo4');
//        表達(dá)式可以簡寫  但是不可以省略控制器
//        return $this->view->engine->layout(true)->fetch('index/demo4');
//        關(guān)閉模版布局
//        return $this->view->engine->layout(false)->fetch('index/demo4');
       //自定義模版  layout可以是任意的布局模版  被替換的內(nèi)容標(biāo)識(shí)符也可以替換

       return $this->view->engine->layout('layout')->fetch('index@index/demo4');

五、模版繼承

//在view層下新建base.html作為父模版
//在父模版里只有{block name=""}{/block}里面的才能顯示
//子模版中直接用{extend name="" /}
return $this->view->fetch();

六、常用標(biāo)簽及分頁功能

{// 模版中兩種注釋的區(qū)別}
{// 這種在源碼中看不到}
<!--這種在源碼中可以看到哦-->
{//其實(shí)模板可以繞過控制器,直接獲取數(shù)據(jù)}
{//assign標(biāo)簽可以在模板中直接定義變量}
{//assign name="staffs" value=":\app\index\model\Staff::where('salary', '<', 3000)->select()" /}
{//name值的獲取過程可以用助手函數(shù)model進(jìn)行簡化,這也是推薦的方式}
{assign name="staffs" value=":model('staff')::where('salary', '<', 1)->select()" /}
{//assign name="staffs" value=":model('staff')::all()" /}
{// 一、 foreach標(biāo)簽 類似于原生的foreach語句}
<!--{//foreach $staffs as $staff}
<tr>
   <td>{//$staff.id}</td>
   <td>{//$staff.name}</td>
   <td>{//$staff.sex}</td>
   <td>{//$staff.age}</td>
   <td>{//$staff.salary}</td>
</tr>
{// /foreach}-->
{//二、volist循環(huán)標(biāo)簽  使用最廣泛 參數(shù)最多  功能強(qiáng)大}
{//基本用法 name="變量名,與控制器相符不可更改" id="循環(huán)變量 可自定義"}
{//volist name="staffs" id="staff"}
{//擴(kuò)展用法  獲取指定范圍內(nèi)的數(shù)據(jù)信息 offset="起始索引" length="數(shù)據(jù)數(shù)量"}
{//volist name="staffs" id="staff" offset="0" length="2"}
{//擴(kuò)展用法  獲取數(shù)據(jù)的奇偶行 mod="2" 數(shù)據(jù)索引除以2得到的商在判斷}
{//eq 上面除以2得到商為1是偶數(shù)  例如3為第四個(gè)數(shù)據(jù) 余數(shù)是1}
{//volist name="staffs" id="staff" length="10" mod="2"}
{//eq name="mod" value="1"}
<!--<tr>
   <td>{//$staff.id}</td>
   <td>{//$staff.name}</td>
   <td>{//$staff.sex}</td>
   <td>{//$staff.age}</td>
   <td>{//$staff.salary}</td>
</tr>-->
{///eq}
{///volist}
{//三、 擴(kuò)展用法  數(shù)據(jù)集合為空}
{//volist name="staffs" id="staff" empty="沒有數(shù)據(jù)"}
{//這種的很少用 一般都用的是empty標(biāo)簽}
{empty name="staffs"}
<h3 style="color: #c7254e">沒有任何數(shù)據(jù)</h3>
{else /}
{volist name="staffs" id="staff"}
<tr>
   <td>{$staff.id}</td>
   <td>{$staff.name}</td>
   <td>{$staff.sex}</td>
   <td>{$staff.age}</td>
   <td>{$staff.salary}</td>
</tr>
{/volist}
{/empty}
{///volist}

  • 范圍取值及分頁

  • {foreach name="staffs" id="staff"}
       <tr>
           <td>{$staff.id}</td>
           <td>{$staff.name}</td>
           <td>
               {// in 里面的值是離散的 意思就是一個(gè)個(gè)的}
               {in name="staff.sex" value="0,1"}
                   {if $staff.sex == 1}
                       男
                   {else /}
                       女
                   {/if}
               {/in}

           </td>
           <td>
               {//$staff.age}
               {// between 里面的值是連續(xù)的}
               {between name="staff.age" value="20, 30"}
                   很年輕
               {/between}

               {between name="staff.age" value="31, 55"}
                   正值壯年
               {/between}
               {between name="staff.age" value="56,70"}
                   最美夕陽紅
               {/between}
               {between name="staff.age" value="70,100"}
                   不服老不行
               {/between}
           </td>
           <td>{$staff.salary}</td>
       </tr>
       {/foreach}
    </table>
    <div class="text-center">{$page|raw}</div>

批改狀態(tài):未批改

老師批語:
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)