1. 下载最新的smarty。
2. 下载的smarty内核文件夹libs,放入php网站文件夹内。(安全起见,可自行修改文件夹名,如改名为smarty)
3. 分别在网站目录下建立templates、templates_c、configs、cache四个文件夹。
4. 在templates/目录下写模板,建立index.htm内容为:
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- {$vice}
- </body>
- </html>
OK,我们来测试一下,php调用smarty,编写如下程序:
- <?php
- include('smarty/Smarty.class.php');
- //程序目录
- const DIR_SEP = DIRECTORY_SEPARATOR;
- define('SITE_ROOT', dirname(__FILE__).DIR_SEP);
- $smarty = new Smarty;
- $smarty->template_dir = SITE_ROOT.'templates'.DIR_SEP;
- $smarty->complie_dir = SITE_ROOT.'templates_c'.DIR_SEP;
- $smarty->config_dir = SITE_ROOT.'configs'.DIR_SEP;
- $smarty->cache_dir = SITE_ROOT.'cache'.DIR_SEP;
- $smarty->assign(vice,'hello vice!');
- $smarty->display('index.htm');
- ?>
<?php include('smarty/Smarty.class.php'); //程序目录 const DIR_SEP = DIRECTORY_SEPARATOR; define('SITE_ROOT', dirname(__FILE__).DIR_SEP); $smarty = new Smarty; $smarty->template_dir = SITE_ROOT.'templates'.DIR_SEP; $smarty->complie_dir = SITE_ROOT.'templates_c'.DIR_SEP; $smarty->config_dir = SITE_ROOT.'configs'.DIR_SEP; $smarty->cache_dir = SITE_ROOT.'cache'.DIR_SEP; $smarty->assign('world','hello world!'); $smarty->display('index.htm'); ?>
输出结果:hello vice!