- 自定义模板引擎类
类-》把html和php分离
(1)给模板分配数据
(2)加载模板
(3)把需要替换的占位符字符串和替换以后的值存储在数组里
(4)替换
File_get_contents();读取模板内容
Str_replace(需要替换的占位符,替换以后值,读取出来的模板);
- 搭建系统Smarty
(1)导入系统Smarty类
(2)类的实例化
(3)初始化参数信息
(4)给模板分配数据
(5)加载模板
- Smarty的语法
分配数据:整形 字符串 数组 一维数组 索引数组和关联数组
二维数组
注释: 双* 例如{$title}=>{*$title*}
------------------------------------------------------
- Smarty配置信息的获取(重点)
(1)获取配置文件信息一
<1>加载配置文件信息
{config_load file=”配置文件” [section]=””} section 配置文件的区域块
<2>双#号
(2)通过smarty的系统变量
{config_load file=”配置文件” [section]=””} section 配置文件的区域块
格式: {$smarty.config.变量}
- 国际化网页(重点)
{config_load file=”” section=”配置文件所属区域”}
- Foreach 内置标签或者内置函数-》数据遍历(重点)
格式:
{foreach 变量 as 循环出来的变量}{/foreach}
{include file=”被包含的模板”} 模板包含
smarty_02
1.搭建系统Smarty(重点)
(1)导入系统的Smarty类
(2)实例化Smarty类
(3)初始化参数
{ 左定界符
} 右定界符
作用:解析变量
(4)分配数据(给模板变量分配数据)
(5)加载模板
{foreach}{/foreach} 内置函数或者是内置标签,遍历数据
格式:{foreach $data as 循环出来的变量}{/foreach}
2.Smarty语法(重点)
分配数据 字符串 整形 数组 对象
3.Smarty获取配置信息(重点)
(1)为什么要获取配置信息
支付宝接口,获取配置文件信息(了解)
微信支付接口,获取配置文件信息(了解)
发送邮件接口,获取配置信息(了解)
(2)如何获取
<1>方法一:
a.加载配置文件
{config_load file="待加载配置文件名字" section="区域块名"}
b.用双#获取配置文件信息
<2>方法二:
a.加载配置文件
{config_load file="待加载配置文件名字" section="区域块名"}
b.通过系统变量 $smarty
4.学生信息管理模块(重点)
{include file="被包含的模板"} 包含模板内置函数
<?php
//导入init.php
require("init.php");
//导入config.php
require("./configs/config.php");
require("./model/Model.class.php");
//实例化
$mod=new Model("stu");
//获取参数
$a=isset($_GET['a'])?$_GET['a']:"index";
switch($a){
case 'index':
//获取数据
$list=$mod->select();
//分配变量
$smarty->assign('list',$list);
//加载模板
$smarty->display("Stu/index.html");
break;
//添加
case "add":
//加载添加模板
$smarty->display("Stu/add.html");
break;
//执行添加
case 'insert':
if($mod->insert($_POST)){
// echo "数据插入成功";
$smarty->assign('info',"数据插入成功");
}else{
// echo "error";
$smarty->assign('info',"数据插入失败");
}
//加载模板
$smarty->display("Stu/info.html");
break;
//执行删除
case 'delete':
if($mod->delete($_GET['id'])){
$smarty->assign('info',"数据删除成功");
}else{
$smarty->assign('info',"数据删除失败");
}
//加载模板
$smarty->display("Stu/info.html");
break;
//修改
case 'edit':
//获取需要修改的数据
$stu=$mod->find($_GET['id']);
$smarty->assign('stu',$stu);
$smarty->display("Stu/edit.html");
break;
//执行修改
case 'update':
$s=$mod->update($_POST);
if($s){
$smarty->assign('info',"数据修改成功");
}else{
$smarty->assign('info',"数据修改失败");
}
$smarty->display("Stu/info.html");
break;
}
?>
<?php
//导入Smarty
require("./libs/Smarty.class.php");
//实例化
$smarty=new Smarty();
//初始化参数
//模板存放目录(手动创建)
$smarty->template_dir="./templatess";
// 编译目录(自动生成的目录)
$smarty->compile_dir="./templatess_c";
//左定界符
$smarty->left_delimiter="{";
//右定界符
$smarty->right_delimiter="}";
//配置文件的目录 (手动创建)
$smarty->config_dir="./configs";
?>
5.模板继承
(1)为什么要使用
框架
(2)怎么去使用
{extends file="继承模板"} 继承
{block name="标记名字"}{/block} 占位