本文实例讲述了smarty模板配置。分享给大家供大家参考,具体如下:

smarty简介

smarty是一个php模板引擎。更准确的说,它分开了逻辑程序和外在的内容,提供了一种易于管理的方法。可以描述为应用程序员和美工扮演了不同的角色,因为在大多数情况下,他们不可能是同一个人。

配置方法

前话:smarty使用一个名为’smarty_dir’的php常量作为它的系统库目录。基本上,如果你的应用程序可以找到smarty.class.php文件,你不需要设置smarty_dir,smarty将会自己运作。但是,如果 smarty.class.php没有在你的include_path(php.ini里的一项设置)里,或者没有在你的应用程序里设置它的绝对路径的时候,你就必须手动配置smarty_dir了(大多数程序都如此)smarty_dir必须包含结尾斜杠(‘/’)。

正文:下载smarty压缩文件,解压到php网站根目录,可以提前看看demo文件夹内的内容,强烈推荐你为每个用到smarty的应用程序设置单一的目录(如同smarty安装包里的demo的文件结构)! 并且在smarty跟目录下创建cache(缓存文件),template(模板目录),template_c(模板编译后的目录),创建一个example.php文件。其中example.php.内容如下所示:

<?php
//引入smarty核心类文件
require_once("./libs/smarty.class.php");
//实例化smarty对象
$smarty = new smarty();
//设置模板的标签标识
$smarty->left_delimiter = "<{";
$smarty->right_delimiter = "}>";
//设置变量和值
$smarty->assign('helloworld',10000);
//引用模板文件
$smarty->display('example.tpl');

代码写好以后,在template文件下创建example.tpl文件,写上<{$helloworld}>。最后在浏览器下打开example.php,10000将会被解析出来。