使用smarty(第二步)

继第一步。

1. 在template文件夹中创建index.html文件,也就是创建模板文件

2. 在index.php文件中创建一个变量,例如

// 分配一个变量,参数1是变量名,参数2是值
$smarty->assign('a','我是一个字母aaaa');
$smarty->assign('a2',22222222);

3. 那么在index.html中即可调用该变量

输出变量的值:{$a}
<hr>

4. 浏览器输出结果如下

 -------------------------------------------------------------其他使用

5.定义数组

$data = array(
'title' => '今年开始,体检信息为电脑录入,系统信息与泉州校区完全分开。',
'content' => array(
'title1' => '校内新闻',
'title2' => '校外新闻',
),
);
// 把数组分配给变量
$smarty->assign('data1',$data);

在index.html调用

输出数组的值:
{$data1.content.title2}

6. 设置Cookies 和 Session

// 设置cookie
setcookie('username','mr_wong',time()+3600,'./');

// 设置session
session_start();
$_SESSION['user'] = '颜茵';

在index.html调用

<hr>
输出setcookie的值: {$smarty.cookies.username}
<hr>
输出session的值: {$smarty.session.user}
<hr>

7. 获得GET参数,在Index.html页面获得

输出GET传递的参数id:{$smarty.get.id}

8. 定义常量

// 定义常量(temp路径)
$str = str_replace('\\', '/', dirname(__FILE__));
define('TEMP_PATH', $str.'/temp');

在Index.html输出

输出常量的值(输出temp文件夹路径): {$smarty.const.TEMP_PATH}
<hr>

 

posted @ 2015-12-09 00:09  天津饭2015  阅读(146)  评论(0编辑  收藏  举报