0%

PHP Smarty模板引擎的使用

Smarty模板引擎可以把UI与程序分离,程序员可以和美工分工合作,提高工作效率。 第一步:下载Smarty,把libs目录解压到项目根目录,在根目录创建四个目录:cache,configs,templates,templates_c。 第二步:写模板 在templates新建模板文件index.html(默认是tpl后缀,但可以改成html,以便编辑器高亮显示),内容如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{$title}</title>
</head>
<body>
{$content}
</body>
</html>

变量标签用花括号括起来。 回到根目录,新建index.php:

<?php
require_once 'libs/Smarty.class.php';
//引入Smarty类
$smarty=new Smarty();
//初始化
$smarty->assign("title","标题");
$smarty->assign("content","内容");
//设置变量
$smarty->display("index.html");
//显示

?>