Heim  >  Artikel  >  Backend-Entwicklung  >  ThinkPHP框架视图详细介绍 View 视图-模板(9)

ThinkPHP框架视图详细介绍 View 视图-模板(9)

WBOY
WBOYOriginal
2016-06-13 12:11:171191Durchsuche

ThinkPHP框架视图详细介绍 View 视图--模板(九)

视图也是ThinkPHP使用的核心部分:


一、模板的使用

a、规则
模板文件夹下[TPL]/[分组文件夹/][模板主题文件夹/]和模块名同名的文件夹[Index]/和方法名同名的文件[index].html(.tpl)

 -->更换模板文件的后缀名(修改配置文件)

'TMPL_TEMPLATE_SUFFIX'=>'.tpl',//更改模板文件后缀名,默认是html


b、修改模板文件目录层次
Tpl/Index/index.html   ==>   Tpl/Index_index.html (改成这种文件目录形式)
      配置文件:
'TMPL_FILE_DEPR'=>'_',//修改模板文件目录层次

c、模板主题(就是网站的不同模板类似QQ装扮皮肤)
-->TP默认是没有主题的,现在模拟建立2个主题模板 my 和 your 文件夹
  Home/Tpl/my/Index/index.html
  Home/Tpl/your/Index/index.html
  想用哪个模板主题,在配置文件中换上对应值即可  
'DEFAULT_THEME'=>'your',  //设置默认模板主题,访问的时候就是your模板下的index
需要在TPL下面新建一个your文件夹作为模板主题文件夹

*如何动态修改模板主题?
1、在后台准备一个功能,修改config.php文件中的默认模板项
2、通过url传递 t=主题 参数可以修改不同的模板
'DEFAULT_THEME'=>'your',//设置默认模板主题
'TMPL_DETECT_THEME'=>true,//自动侦测模板主题
'THEME_LIST'=>'your,my',//支持的模板主题列表要先建立好 my 和 your模板
设置好后这么访问,后面直接带个t参数  t/你的模板文件:
 http://localhost/thinkphp/index.php/Index/index.html/t/my 
 http://localhost/thinkphp/index.php/Index/index.html/t/your


二、输出模板内容
a、display
1.display中没有参数
$this->display();
2.可以带参数
$this->display(本模块文件夹下的其他模板文件);
$this->display('index2');


$this->display(其他文件夹下的模板文件);
$this->display('User:index');  //本模块调用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位置在网站根目录下 /public/error.html
//几乎不使用
$this->display('./Public/error.html','utf-8','text/xml');  //模板   编码  显示方式(html或xml)


$content ='这是show的使用直接传html代码';//可能是数据库啊直接拿到的输出来
$this->show($content);    ==  $this->show('这是show的使用直接传html代码');  

3.fetch方法
获得模板文件中的内容,以字符串形式返回
$content=$this->fetch('Public:error');
4.show方法
不需要模板文件,可以直接输出模板内容
$content=$this->fetch('Public:error');
dump($content);
$content=str_replace('h1','i',$content);
$this->show($content);

三、模板中的赋值 : 
//$this->assign('name','乐杨俊');  或
$this->name='乐杨俊2';
$this->display();
模板变量输出:
一、变量输出
1.标量输出  --整型 浮点型 字符型、、、
2.数组输出
$arr = array('k1'=>'leyangjun','k2'=>'leyangjun2');
$this->assign('name',$arr);
$this->display();
模板输出用:
{$name[1]}    --索引数组
{$name['k2']} --关联数组  或
{$name.k1}
3.对象输出
{$name:k}
{$name->k}
二、系统变量(手册详细介绍)
//http://localhost/thinkphp/index.php/Index/add/name/leyangjun 
{$Think.get.name}   --url传的name值,在模板中可以直接拿到输出
{$Think.get.id}
三、使用函数(模板中对变量直接使用PHP函数)
{$name|strtoupper} 生成的编译后文件是 在生成临时文件可见
{$name|date='Y m d H:i:s',###}
四、默认值
{$name|default='这里是默认值'}
五、运算符
+ - * / % ++ --
$this->assign('name',10);
{$name++}   --11
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn