牛骨文教育服务平台(让学习变的简单)

PHP Smarty环境配置

Smaty优点:1.代码分离 2.缓存技术

使用步骤:

1.下载Smaty模板

2.将模板中那个lib文件夹复制到项目中(一般为根目录,并且重命名在此命名为Smarty),

3.配置PHP

1.新建一个Smarty_inc.php文件

2.输入

Include_once("Smarty/Smarty.class.php");//包含Smarty类

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

//配置

$smarty->config_dir="Smaryt/Config_File.class.php";

配置文件目录路径,一般不更改

$smarty->caching=false;

是否开启缓存

$smarty->template_dir="./templates";

设置模板目录,手动创建,./代表当前目录必须设置

$samrty->complie_dir="./templates_c";

设置编译目录,手动创建,必须设置

$smarty->cache_dir="./smarty_cache";

设置缓存目录

$smarty->left_delimiter="{";$smarty->right_delimiter="}";

设置在HTML标志Smarty的开始和结束标签自定义也可以世{%%}

Smarty使用变量

定义变量

$smarty->assign("模板变量","值/数组");

将改变量应用到特定的模板中

$smarty->display("模板名");

Eg:

$smarty->assign("title","我的第一个smarty程序");

$smarty->display("index.html");

在index.html中使用

<html>

<head>

<title>{$title}</title>

</head>

</html>

上述一次只能访问一个变量那么怎样循环访问多个变量呢?**

要想访问多个变量必须用到数组

定义


$array={name->"jiangtong",name->"张三",name->"李四"}

$smarty->assign($student,$name);

$smarty->display("index.html");

index.html中使用

{section name="stuList"  loop="$student"}

loop代表循环那个数组

<p>{$student[stuList].name}</p>

 

<sectionelse>

//可以不写

 

{/section}

 

Smarty取出当前时间

{$smarty.now}