有没有为ASP.NET提供的母板页感到兴奋?但是转过头PHP有没有母版呢?smarty模板引擎为你解决了问题。
首先,我们要到smarty的官方网站上下载smarty(http://www.smarty.net/download)下载完后,我们解压里面的lib文件夹到网站根目录下,更名为smarty(具体名称自己喜欢改什么就改什么)。smarty的部署工作就完成啦!
下面便是如何使用smarty母板引擎了(具体smarty高深的我再此不提),新建四个文件夹到网站根目录:cache、configs、templates、templates_c,把你的模板文件命名为master.tpl,保存至templates文件夹下。这是我的master.tpl文件。
<html>
<head>
<title><{$title}></title>
</head>
<body>
<{$content}>
</body>
</html>
<{$title}>便是我们留下的页面标题写的地方。
<{$content}>便是我们留下的页面正文写的地方。
下面我们来看看test.php。
<?php
include_once('./smarty/Smarty.class.php');
$smarty=new Smarty();
$smarty->debuging = true;
$smarty->caching = false;
$smarty->template_dir = "./templates/";
$smarty->compile_dir = "./templates_c/";
$smarty->config_dir = "./configs/";
$smarty->cache_dir = "./cache/";
$smarty->cache_lifetime = 120;
$smarty->left_delimiter = "<{";
//<{便是smarty要编译时使用的开始符号"<{"$title}>
$smarty->right_delimiter = "}>";
//}>便是smarty要编译时使用的结束符号<{$title"}>";
$smarty->assign("title", "主页");
$smarty->assign("content");
//assign是修改模板文件中的smarty变量,比如我们在模板文件中有一个$title,便可以在assign中修改。
$smarty->display("master.tpl");
//display是导入模板文件。
?>
好,我们的第一个使用smarty制作模板的PHP网页就做好啦!赶快看自己的成果吧!