站长新动力

Smarty配置(Smarty 2.6.20)及简单使用图文教程

最近做的糖尿病系统项目需要用到Smarty(不要告诉我你不知道什么是Smarty?),很久之前学习过了的,但现在也忘的差不多了,所以再来温习温习。

这里有一个好消息,就是Smarty 3.0于2008年10月17日已经开始内测了,之前很多人都担心Smarty的更新问题,现在我想大家应该可以放心使用了,相信Smarty在3.0版本会带给我们更多惊喜。下面是官方网站的说明:

Smarty 3 Alpha has begun!

[17-Oct-2008] The Smarty 3.0 engine is well on its way, morphing every day. The alpha branch SVN is available to browse here, and the README for it is here. Feel free to contribute your thoughts in the forum. This is alpha code, meaning it is not complete. Don’t try to use this for production! Once the feature set is complete, it will move to beta.

You can checkout Smarty 3 Alpha with SVN:
svn checkout http://smarty-php.googlecode.com/svn/branches/Smarty3Alpha/

See also the dev mailing list:
http://groups.google.com/group/smarty-developers

本文将详细讲解 Smarty 2.6.20 的配置及简单使用!

1、Smarty 官方网站 下载Smarty (Smarty 下载页面) Smarty 2.6.20 ;

2、将下载下来的压缩文件解压,将libs文件夹放到WWW/test/目录下(根据实际使用环境解压到网站目录相应的位置);压缩包中其他的文件夹(包括演示及测试文件夹等)都可以删除掉。WWW/test/目录为我们测试smarty使用的目录。

3、Smarty的配置:

3.1、在test目录下再建立如下几个文件夹(一般程序员都这么建,所以我也这么建):

cache —— 缓存文件夹
templates —— 模板文件夹
templates_c —— 模板编译文件夹

3.2、配置Smarty。在test目录下建立Smarty_config.php文件,主要是配置Smarty用;内容如下:

include_once(”libs/Smarty.class.php”); //包含smarty类文件

$smarty = new Smarty(); //建立smarty实例对象$smarty

$smarty->config_dir=”libs/Config_File.class.php”; // 目录变量

$smarty->caching=false; //是否使用缓存,项目在调试期间,不建议启用缓存

$smarty->template_dir = “./templates”; //设置模板目录

$smarty->compile_dir = “./templates_c”; //设置编译目录

$smarty->cache_dir = “./cache”; //缓存文件夹

//一下两行定义左右边界符,默认为{},但实际应用当中容易与JavaScript相冲突,建议修改为自己习惯的边界符号

$smarty->left_delimiter = “<!–{”;

$smarty->right_delimiter = “}–>”;

4、Smarty 应用:

4.1、Smarty 使用变量

4.1.1、$smarty->assign(”模板变量”, “值(数组/变量)”); 给模板变量赋值,如:$smarty->assign(”title”, “学习Smarty页面”);
4.1.2、$smarty->display(”模板名称”); 引入模板文件,如index.html

4.2、Smarty 循环数组

Smarty循环数组使用Smarty内置的方法section。

$smarty->assign(”模板变量”, “数组”);

<!–{section name=循环的名称 loop=需要循环的模板变量}–>

<!–{$stu[s].name}–>//$stu=需要循环的模板变量 ,s=循环的名称

<!–{/section}–>

5、简单是Smarty使用实例。

在test/templates 目录下建立index.html文件,内容如下:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title> <!–{$title}–> </title>
<meta http-equiv=”Content-type” content=”text/html; charset=utf-8″>
<META NAME=”Author” CONTENT=”">
<META NAME=”Keywords” CONTENT=”">
<META NAME=”Description” CONTENT=”">
</head>

<body>
此页面的标题是: <!–{$title}–><br />
此页面测内容是:<!–{$content}–><br />
<br />
测试Smarty数组循环:<!–{$testarray}–><br />
<!–{section name=testas loop=$testarray}–>
<!–{$testarray[testas].step1}–>  <!–{$testarray[testas].step2}–>  <!–{$testarray[testas].step3}–><br />
<!–{/section}–>
</body>
</html>

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

<?php
include_once(’Smarty_config.php’);//包含Smarty配置文件

//$smarty->assign(’title’,'Smarty配置(Smarty 2.6.20)及简单使用图文教程’);//给模板变量赋值,还可以将变量赋值给模板变量,如下;

$title = “Smarty配置(Smarty 2.6.20)及简单使用图文教程”;
$smarty->assign(’title’,$title);//给模板变量赋值
$content = “最近做的糖尿病系统项目需要用到Smarty(不要告诉我你不知道什么是Smarty?),很久之前学习过了的,但现在也忘的差不多了,所以再来温习温习。这里有一个好消息,就是Smarty 3.0于2008年10月17日已经开始内测了,之前很多人都担心Smarty的更新问题,现在我想大家应该可以放心使用了,相信Smarty在3.0版本会带给我们更多惊喜。”;
$smarty->assign(’content’,$content);//给模板变量赋值

$testarray =array(array(’step1′=>’1 Smarty模板使用第一步’,’step2′=>’1 Smarty模板使用第二步’,’step3′=>’1 Smarty模板使用第三步’),
array(’step1′=>’2 Smarty模板使用第一步’,’step2′=>’2 Smarty模板使用第二步’,’step3′=>’2 Smarty模板使用第三步’),
array(’step1′=>’3 Smarty模板使用第一步’,’step2′=>’3 Smarty模板使用第二步’,’step3′=>’3 Smarty模板使用第三步’),
array(’step1′=>’4 Smarty模板使用第一步’,’step2′=>’4 Smarty模板使用第二步’,’step3′=>’4 Smarty模板使用第三步’)
);

$smarty->assign(’testarray’,$testarray);

$smarty->display(’index.html’);//加载模板文件

你可以将上述文件拷贝到电脑中进行测试,或者自己写出更好的测试文件来!

当然Smarty还有更多的知识,以上只是简单的例子,更多学习资料可以到网上下载Smarty手册或者看这里的在线手册:http://www.hbcms.com/main/smarty/

 

  • 本文来自:LAMP技术学习博客
  •       http://www.dvdxin.com/ 
    欣欣影院
  • 文章标题:Smarty配置(Smarty 2.6.20)及简单使用图文教程
  • 文章地址:http://hellolamp.com/595.html

posted on 2008-12-03 11:47  Zhan86  阅读(1181)  评论(0)    收藏  举报

导航