smarty是php的模板引擎,目的是分离业务逻辑和显示逻辑。
ok,下面举例怎么在项目中引入smarty。
1、下载smarty
http://www.smarty.net/download
注意php版本和smarty的匹配
2、解压smarty,把lib目录复制粘贴到网站根目录下,改名lib为smarty
3、在网站根目录创建文件夹templates和template_c
4、创建test.php
<?php
include_once("./smarty/smarty.class.php");
$smarty = new Smarty();
$smarty->template_dir = "./templates";
$smarty->compile_dir = "./templates_c";
$smarty->left_delimiter = "{{";
$smarty->right_delimiter = "}}";
$smarty->assign('username','雨田');
$smarty->display("test.html");
?>
5、在templates下面创建test.html
<!DOCTYPE html>
<html>
<head>
<title>My First Page</title>
</head>
<body>
My name is {{$username}}
</body>
</html
6、运行test.php
显示如下
My name is 闆ㄧ敯
咦,有乱码。。。。
解决乱码问题:
在test.php中加入
header("Content-Type:text/html;charset=utf-8");
ok,在运行下test.php,显示正常,乱码问题解决
My name is 雨田