当前位置: 首页 > 图文教程 > 网络编程 > PHP > PHP教程:配置smarty开发环境

PHP
新手入门:学习掌握动态网页PHP的编程语句
PHP建设论坛:Discuz!论坛快速架设指南
理解动态网页技术PHP与数组的应用
PHP初学:实例详细学习PHP的简单语法
PHP实例:PHP生成带有雪花背景的网站验证码
PHP网站开发中关于包含路径问题的解决方案
用PHP实现网页开发中的翻页跳转
用PHP程序实现随机广告图片显示
如何使PHP文件与HTML代码更好的分离
PHP关于代码转换问题比较完善的解决办法
新手如何使用PHP创建RSS阅读器
用PHP程序为自己网站打造一个搜索引擎
PHP实现文件安全下载的程序
快速掌握MySQL数据库中SELECT语句
用javascript+php随机显示图片
论Web 2.0 时代PHP的地位
用动态网页技术PHP打造个人网站全攻略
问题解决:无法载入MYSQL扩展,请检查PHP配置
新手入门:PHP网站开发中常见问题汇总
用PHP程序实现删除目录的三种方法实例

PHP教程:配置smarty开发环境


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-04   浏览: 46 ::
收藏到网摘: n/a

首先到 http://www.smarty.net 上下载最新的smarty模板引擎,解压Smarty-2.6.26.zip,改名Smarty-2.6.26目录为smarty。

拷贝smarty目录到你希望的目录 D:\xampp\xampp\smarty。

在php.ini的include_path加入smarty库目录,如下:

include_path = “.;D:\xampp\xampp\php\PEAR;D:\xampp\xampp\smarty\libs”

在你的php项目目录新建两个子目录放配置文件和模板:config 和templates

D:\xampp\xampp\htdocs\config

D:\xampp\xampp\htdocs\templates

smarty项目目录新建两个目录cache和templates_c存放缓存和编译过的模板:

D:\xampp\xampp\smarty\cache

D:\xampp\xampp\smarty\templates_c

在需要调用smarty库的php文件中写入代码:

1
2
3
4
5
6
7
8
9
10
11
//this is D:\xampp\xampp\htdocs\index.php
//load smarty library
require('Smarty.class.php');

$smarty=new Smarty();
$smarty->template_dir='d:/xampp/xampp/htdocs/templates'; //指定模板存放目录
$smarty->config_dir='d:/xampp/xampp/htdocs/config';//指定配置文件目录
$smarty->cache_dir='d:/xampp/xampp/smarty/cache';//指定缓存目录
$smarty->compile_dir='d:/xampp/xampp/smarty/templates_c';//指定编译后的模板目录
$smarty->assign('name','fish boy!');
$smarty->display('index.tpl');

再新建一个D:\xampp\xampp\htdocs\templates\index.tpl文件

1
2
3
4
5
6
7
8
9
10
<html>
<head><title>hello,{$name}!</title>
<script language="javascript" type="text/javascript">
    alert('{$name}');
</script>
</head>
<body>
hello,{$name}!
</body>
</html>

打开http://localhost/index.php 应该会弹出fish boy!警告,然后内容为hello,fish boy!!的页面。
我们可以改进一下,不可能每次需要smarty写这么多配置代码吧。
新建文件 D:\xampp\xampp\htdocs\smarty_connect.php

1
2
3
4
5
6
7
8
9
10
11
//load smarty library
require('Smarty.class.php');
class smarty_connect extends Smarty
{   function smarty_connect()
    {//每次构造自动调用本函数
        $this->template_dir='d:/xampp/xampp/htdocs/templates';
        $this->config_dir='d:/xampp/xampp/htdocs/config';
        $this->cache_dir='d:/xampp/xampp/smarty/cache';
        $this->compile_dir='d:/xampp/xampp/smarty/templates_c';
    }
}

D:\xampp\xampp\htdocs\index.php改为:

1
2
3
4
    require('smarty_connect.php');
    $smt=new smarty_connect;
    $smt->assign('name','fish boy!');
    $smt->display('index.tpl');

index.tpl文件不变,打开localhost/index.php,出现了同样的输出。