smarty教程-smarty入门基础应用实例教程
/*
smarty选用的一种缓存技术,它可以将用户最终看到的HTML文件缓存成一个静态的HTML页,当设定smarty的cache属性为true时,在smarty设定的cachetime期内将用户的WEB请求直接转换到这个静态的HTML文件中来,这相当于调用一个静态的HTML文件。
采用smarty编写的程序可以获得最大速度的提高,这一点是相对于其它的模板引擎技术而言的。
*/
$servername = 'localhost';
$dbname='whois';//数据库教程名称,需修改为您当前使用的库名称
$tbname='post';//数据表名称,不用修改
$name='root';//数据库用户名,需修改
$password='xxxxxxx';//数据库链接密码,需修改
require_once 'smarty/libs/Smarty.class.php教程';
$smarty = new Smarty(); //初始化类
$smarty->template_dir = "smarty/templates/templates"; //设置模板目录
$smarty->compile_dir = "smarty/templates/templates_c"; //设置编译目录
$smarty->config_dir = "smarty/templates/config"; //配置文件目录
$smarty->cache_dir = "smarty/templates/cache"; //
$smarty->caching = false;
$db=mysql教程_connect($servername,$name,$password)or die ('服务器连接错误。');
$ii=mysql_select_db("$dbname")or die("不能选择数据库");
mysql_query("set names 'gbk'");$title ='smarty 教程';
$keyword ='smarty模板,smarty教程';
$description ='提供简单的smarty入门教程';
$smarty->assign('title',$title);
$smarty->assign('keyword',$keyword);
$smarty->assign('description',$description);$smarty->display('index.html'); //使用 smartytemplatestemplatesindex.html 模板
?>
index.html模板文件
<title><!--{$title}--></title>
<meta name="keywords" content="<!--{$keyword}-->" />
<meta name="description" content="<!--{$description}-->" />{include file='head.tpl'}
{include_php file='body.php'}
{include file='footer.tpl'}
<?
/*
smarty 模板变量
.{$name}: 模板变量,smarty中的核心组成,采用smarty定义的左边界符{与右边界符}包含着、以PHP变量形式给出,在smarty程序中将使用
$smarty->assign("name", "李晓军");将模板中的$name替换成“李晓军”三个字。
*/