視圖也是ThinkPHP使用的核心部分:
一、模板的使用
a、規(guī)則
模板文件夾下[TPL]/[分組文件夾/][模板主題文件夾/]和模塊名同名的文件夾[Index]/和方法名同名的文件[index].html(.tpl)
-->更換模板文件的后綴名(修改配置文件)
'TMPL_TEMPLATE_SUFFIX'=>'.tpl',//更改模板文件后綴名,默認(rèn)是html
b、修改模板文件目錄層次
Tpl/Index/index.html ==> Tpl/Index_index.html (改成這種文件目錄形式)
配置文件:
'TMPL_FILE_DEPR'=>'_',//修改模板文件目錄層次
c、模板主題(就是網(wǎng)站的不同模板類似QQ裝扮皮膚)
-->TP默認(rèn)是沒有主題的,現(xiàn)在模擬建立2個主題模板 my 和 your 文件夾
Home/Tpl/my/Index/index.html
Home/Tpl/your/Index/index.html
想用哪個模板主題,在配置文件中換上對應(yīng)值即可
'DEFAULT_THEME'=>'your', //設(shè)置默認(rèn)模板主題,訪問的時候就是your模板下的index
需要在TPL下面新建一個your文件夾作為模板主題文件夾
*如何動態(tài)修改模板主題?
1、在后臺準(zhǔn)備一個功能,修改config.php文件中的默認(rèn)模板項
2、通過url傳遞 t=主題 參數(shù)可以修改不同的模板
'DEFAULT_THEME'=>'your',//設(shè)置默認(rèn)模板主題
'TMPL_DETECT_THEME'=>true,//自動偵測模板主題
'THEME_LIST'=>'your,my',//支持的模板主題列表要先建立好 my 和 your模板
設(shè)置好后這么訪問,后面直接帶個t參數(shù) t/你的模板文件:
http://localhost/thinkphp/index.php/Index/index.html/t/my
http://localhost/thinkphp/index.php/Index/index.html/t/your
二、輸出模板內(nèi)容
a、display
1.display中沒有參數(shù)
$this->display();
2.可以帶參數(shù)
$this->display(本模塊文件夾下的其他模板文件);
$this->display('index2');
$this->display(其他文件夾下的模板文件);
$this->display('User:index'); //本模塊調(diào)用User模塊的index文件
//注意,僅僅需要在Tpl下有Public文件夾以及其中的error.html即可
//不需要一定有Public模塊--PublicAction.class.php
$this->display('Public:error');
$this->display(其他主題下的 文件夾下的 模板文件);
//需要開啟主題支持,DEFAULT_THEME'=>'my'
$this->display('my:Index:index');
$this->display(一個url路徑);
$this->display('./Public/error.html'); //public位置在網(wǎng)站根目錄下 /public/error.html
//幾乎不使用
$this->display('./Public/error.html','utf-8','text/xml'); //模板 編碼 顯示方式(html或xml)
$content ='<b>這是show的使用直接傳html代碼</b>';//可能是數(shù)據(jù)庫啊直接拿到的輸出來
$this->show($content); == $this->show('<b>這是show的使用直接傳html代碼</b>');
3.fetch方法
獲得模板文件中的內(nèi)容,以字符串形式返回
$content=$this->fetch('Public:error');
4.show方法
不需要模板文件,可以直接輸出模板內(nèi)容
$content=$this->fetch('Public:error');
dump($content);
$content=str_replace('h3','i',$content);
$this->show($content);
三、模板中的賦值 :
//$this->assign('name','樂楊俊'); 或
$this->name='樂楊俊2';
$this->display();
模板變量輸出:
一、變量輸出
1.標(biāo)量輸出 --整型 浮點(diǎn)型 字符型、、、
2.數(shù)組輸出
$arr = array('k1'=>'leyangjun','k2'=>'leyangjun2');
$this->assign('name',$arr);
$this->display();
模板輸出用:
{$name[1]} --索引數(shù)組
{$name['k2']} --關(guān)聯(lián)數(shù)組 或
{$name.k1}
3.對象輸出
{$name:k}
{$name->k}
二、系統(tǒng)變量(手冊詳細(xì)介紹)
//http://localhost/thinkphp/index.php/Index/add/name/leyangjun
{$Think.get.name} --url傳的name值,在模板中可以直接拿到輸出
{$Think.get.id}
三、使用函數(shù)(模板中對變量直接使用PHP函數(shù))
{$name|strtoupper} 生成的編譯后文件是 <?php echo (strtoupper($name)); ?> 在生成臨時文件可見
{$name|date='Y m d H:i:s',###}
四、默認(rèn)值
{$name|default='這里是默認(rèn)值'}
五、運(yùn)算符
+ - * / % ++ --
$this->assign('name',10);
{$name++} --11
PHP編程 鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。