PHP之Smarty学习

参考网络上的资料,使用Smarty可以简单的测试:

1.解压Smarty到网站目录下,比如起名libs.

2.建立templates,templates_c,configs,cache目录

3.建立测试页面:index.php

<?php

require "main.php";

//$tpl在main.php中定义

$tpl->assign("title", "测试用的网页标题");

$tpl->assign("content", "测试用的网页内容");

// 上面两行也可以用这行代替

// $tpl->assign(array("title" => "测试用的网页标题", "content" => "测试用的网页内容"));

$tpl->display('Index.htm');

?>

4.在templates下建立模板文件index.htm

<html>

  <head>

  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">

  <title><{$title}></title>

  </head>

  <body>

  <{$content}>

  </body>

  </html>

5.建立main.php

<?php

/*下面这加如果不加会报告错误:

Notice: Undefined variable: _SESSION in E:\PHPnow-1.5.1\vhosts\xxh.com\libs\Smarty.class.php on line 585

不知道为什么

*/

session_start();//开启session

include "libs/Smarty.class.php";

define('__SITE_ROOT', 'E:/PHPnow-1.5.1/vhosts/xxh.com'); // 最后没有斜线

$tpl = new Smarty();

$tpl->template_dir = __SITE_ROOT . "/templates/";

$tpl->compile_dir = __SITE_ROOT . "/templates_c/";

$tpl->config_dir = __SITE_ROOT . "/configs/";

$tpl->cache_dir = __SITE_ROOT . "/cache/";

$tpl->left_delimiter = '<{';

$tpl->right_delimiter = '}>';

?>

6.打开index.php即可以测试Smarty了.