1.下载smarty插件,最终会拿到一个libs的文件夹,里面包含几个PHP及几个文件夹,其中有一个PHP叫Smarty.class.php
2.将libs中的Smarty.class.php引入自己的PHP中,也可以当作头文件引入,引入方式类型于
require '../../libs/Smarty.class.php';
3.Smarty可配置四个目录,默认名称分别是 templates/
, templates_c/
, configs/
和 cache/
。
4.可手动配置目录对应位置,注意文件及目录相对路径,配置方式类型于
define("__SITE_ROOT", "D:/workspace-portal/toal/csb_portal/zt/demo");
$tpl = new Smarty;
$tpl->setTemplateDir(__SITE_ROOT . "/templates/");
$tpl->setCompileDir(__SITE_ROOT . "/templates_c/");
$tpl->setConfigDir(__SITE_ROOT . "/configs/");
$tpl->setCacheDir(__SITE_ROOT . "/cache/");
$tpl->left_delimiter = '<{';
$tpl->right_delimiter = '}>';
5.以头文件方式引入smarty框架完整代码
main.php
<?php
require '../../libs/Smarty.class.php';
define("__SITE_ROOT", "D:/workspace-portal/toal/csb_portal/zt/demo");
$tpl = new Smarty;
$tpl->setTemplateDir(__SITE_ROOT . "/templates/");
$tpl->setCompileDir(__SITE_ROOT . "/templates_c/");
$tpl->setConfigDir(__SITE_ROOT . "/configs/");
$tpl->setCacheDir(__SITE_ROOT . "/cache/");
$tpl->left_delimiter = '<{';
$tpl->right_delimiter = '}>';
?>
使用smarty例子,我把它放在htdocs目录下
test.php
<?php
require "main.php";
$tpl->assign("title", "测试标题");
$tpl->assign("content", "测试内容");
$tpl->display('test.htm');
?>
在main.php配置的templates目录中新加test.htm文件,文件后缀名随意,内容类似于
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><{$title}></title>
</head>
<body>
<{$content}>
</body>
</html>
浏览器http://xx.xx.xx/projectname/htdocs/test.php