1.assign赋值
$this->assign('title','模板操作');
$this->assign('bests',$bests);//$bests是二维数组
2.变量的输出
在Html代码中输出标题如下:
<title>{$title}</title>
注意:TP的模板标签和smarty稍有不同
TP在控制结构/循环/if/else时,用<>
而最具体的输出变量 用{$标签}
3.if/else标签
/* //if
public function ve(){
$a = mt_rand(3,6);
$this->assign('ff',$a);
$this->display();
}
*/
/*
//三元运算符
public function ve(){
$a = mt_rand(3,6);
$this->assign('ff',1);
$this->display();
}
// */
//数组
public function ve(){
$a = array(
array('id'=>1,'title'=>'中国123'),
array('id'=>2,'title'=>'中国456'),
array('id'=>3,'title'=>'中国789'),
);
$this->assign('ff',$a);
$this->display();
}
在home/view/index目录下创建ve.html
<include file = "Public/head"/> <body> <!-- <h1> {$ff?'hello world':00000}<br/>
//if的操作 <if condition = "$ff eq 5"> {$ff} <else/>{$ff}000 </if> </h1> -->
//数组的遍历 <foreach name='ff' item="v"> {$v.title}<br/> </foreach> </body> </html>
4.import导入css,js文件
在home/view/Index/ve.html
<head>
<!--
<import type="css" file="a.demo" />
<import type="js" file="cs" />
-->
<load href = "__PUBLIC__/cs.js" />
</head>
<body>
<!--
<h1>
{$ff?'hello world':00000}<br/>
<if condition = "$ff eq 5">
{$ff}
<else/>{$ff}000
</if>
</h1>
-->
<foreach name='ff' item="v">
{$v.title}<br/>
</foreach>
</body>
新建一个Public文件夹
Public里面放入需要引入的文件
5.load导入css,js文件
在home/view下建一个public文件夹
里面是需要引入的头文件
在Home/view/index目录下引入public文件目录下的模块
代码如下
ve.html
<include file = "Public/head"/> <body> </body> </html>
head.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<!--
<import type="css" file="a.demo" />
<import type="js" file="cs" />
-->
<load href = "__PUBLIC__/cs.js" />
</head>
<body>
</body>
</html>
效果图
6.不要让模板做太多的工作!
功能:输出数据,而非处理数据
应该由控制器来处理