Smarty安装教程

smarty的最新网址:http://www.smarty.net/

到这个页面http://www.smarty.net/download.php 下载最新的smarty源文件。

解压下载的文件,把libs文件夹及目录下的所有文件拷贝到的一新的文件夹如:smarty_temp目录下面,然后
再在这个目录下面新建立四个文件夹:templates,templates_c,cache,configs

然后再新建立一个配置文件:config.inc.php,内容如下:


<?php

/*
 * 只要把此文件放在和templates,templates_c,cache,configs同一个目录下,
 * 在其它地方只要调用这个文件,就可以显示出来了。
 *
 */
    // 把D:/phpweb/myweb/smarty_temp路径换成D:/phpweb/myweb/smarty_temp
    $PathArray = explode('smarty_temp',str_replace('//','/',dirname(__FILE__)));
   
    define('ROOT',$PathArray[0]);                    //设置网站文件的绝对路径
    define('SMARTY_ROOT',ROOT.'smarty_temp/');        //设置Smarty类库路径
   
    require_once 'libs/Smarty.class.php';
    $Smarty = new Smarty();                        //建立新的Smarty对象
    $Smarty -> compile_check   = TRUE;            //每次PHP执行时查看模板文件的内容是否改变。
    //debugging控制台,这是一个开关参数,如果打开它将显示Smarty变量和运行状态的调试窗口。
    $Smarty -> debugging       = FALSE;
   
    //设置模板目录
    $Smarty -> template_dir    = SMARTY_ROOT.'templates/';
   
    //Smarty默认的模板目录名:templates_c
    $Smarty -> compile_dir     = SMARTY_ROOT.'templates_c/';
   
    //默认模板缓存的目录名
    $Smarty -> cache_dir       = SMARTY_ROOT.'cache/';
   
    //默认的config文件目录名:configs
    $Smarty -> config_dir      = SMARTY_ROOT.'configs/';
   
    //左右边界符,默认为{},但实际应用当中容易与JavaScript相冲突,所以建议设成<{}>或其它。
    $Smarty -> left_delimiter  = '<{';
    $Smarty -> right_delimiter = '}>';

?>



测试一下:

在tmeplates目录下建立test.html模板文件,内容如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title><{$title}></title>
</head>
<body>
<{$content}>
</body>
</html>

然后在smarty_temp目录下建立test.php文件,内容如下:

<?php
    require_once 'Config.inc.php';//只要能把这个文件包含进来,test.php文件放在哪都可以。
   
    $Smarty->assign("title", "测试成功了,这是标题"); //进行模板变量替换
    $Smarty->assign("content", "测试结果成功"); //进行模板变量替换
    $Smarty->display('test.htm');//编译并显示位于./templates下的index.tpl模板
?>


运行一下就可以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值