PHP引入数据库,数据库内容遍历的显示在HTML页面中嵌套写入时
echo "<ul>"; $db = new MySql; $db ->query(select * from ts_news); $data = $db -> findAll(); foreach($data as $vo){ echo "<li><a href="{$vo['link']}">{$vo['title']}</a></li>" } echo "</ul>"
如果通过模板引擎来嵌套就会是这个样
<div> 模板文件 <ul> {@ newslist@} </ul> </div>
模板引擎:
PHP代码和HTML代码相分离的一种解决方案。
模板引擎优点:
使PHP代码和HTML代码分离,并且使HTML页面更加简洁。
PHP程序员和前端人员并行开发,提高了效率。
网站后期更改网站风格主题更加方便。
模板引擎种类:
Smarty Smart PHPlib ThinkTemplate
Smarty好处:
smarty是纯PHP编写的模板引擎类库。
smarty有丰富的控制结构。
smarty内有丰富的插件机制。
smarty内有丰富的缓存控制,可以加快页面加载速度。
Smarty运行原理:
Smarty在第一次调用模板时,运行速度是慢的,因为,模板文件要经过Smarty的解析类将模板文件编译成编译文件后才能运行,并且如果解析类查询到缓存开启,那么还会通过编译文件生成缓存文件,所以速度比直接运行PHP和HTML代码要慢,但是以后所有调用,会先判断缓存文件是否存在,如果存在就直接调用缓存文件,所以整体速度是快,如果缓存文件不在,会查询是否存在编译文件,如果编译文件存在,直接运行编译文件。如果编译文件不在,再重新编译。
Smarty的使用:
下载smarty类库:www.smarty.net
Smarty2.x: smarty封装内部很多标签和使用都死函数封装的。
Smarty3.x: 使用了纯的面向对象编程。
将类库引入项目中:
初始化smarty对象:
Smarty.class.php:核心解析类
SmartyBC.class.php:2.0到3.0的过渡类
Require_once("./libs/smarty.class.php");
$tpl = new Smarty;
smarty配置:
2.0: 配置编译和模板目录
3.0: 只配置模板目录
$tpl -> template_dir = "./templates/"; 模板目录
$tpl -> compiler_dir = "./compilers"; 编译目录
模板文件后缀可以随意设置: .tp .dwz .html .htm .ts
调用前台模板文件:
$tpl -> display("文件名");
<?php require_once("./libs/Smarty.class.php"); $tpl = new Smarty; $tpl -> template_dir = "./templates/"; $tpl -> compile_dir = "./compilers/"; $tpl -> display("index.html"); ?>
Smarty变量:
php分配给smarty的变量
$tpl -> assign(“前台变量名”,值);
前台访问:
{$前台变量名}
自定义左右定界符:
$tpl -> left_delimiter = "{ts: ";
$tpl -> right_delimiter= "}";
smarty模板变量
{ts:assign var="" value=""}
var: 模板变量的变量名
val: 模板变量的变量值
前台样式配置变量
{ts:$smarty.config.section.变量名}
{ts:#变量名#}
全局变量
{ts:$smarty.session|cookie|get|post|request.键名}
$_session[‘uname’]
{ts:$smarty.session.uname}
【Index.php】
<?php require_once("./libs/Smarty.class.php"); $tpl = new Smarty; $tpl -> template_dir = "./templates/"; $tpl -> compile_dir = "./compilers/"; $tpl -> left_delimiter = "{ts:"; $tpl -> right_delimiter= "}"; $tpl -> assign("title","This is Web Title!"); $tpl -> display("index.html"); ?>
【Index.html】
<h1>Test Template index!</h1> {ts:$title} <hr> {ts:assign var="username" value="TOM"} {ts:$username}
#在Index.php中获取前台Index.html中的值#
Smarty中的控制结构:
分支结构:
if else
{ts:if 条件}
代码块
{ts:else if 条件}
代码块
{ts:else}
代码块
{ts:/if}
【if.php】
<?php // if.php页面 require_once("./smarty.inc.php"); $marks = 88; $tpl -> assign("marks",$marks); $tpl -> display("if.html"); ?>
【if.html】
{ts:/* if.html */} <h1>if</h1> {ts:if $marks >=90} 优秀 {ts:else if $marks>=80} 良好 {ts:else if $marks>=60} 及格 {ts:else} 不及格 {ts:/if}
#在if.php中获取前台if.html中的值#
循环结构: 关联数组最少可以省一次循环
Foreach循环:
格式:
{ts:foreach name="" item="" key="" from=""}
{ts:/foreach}
Name:循环体的名字
Item:被遍历出的数组元素
Key: 数组元素的键名或者是下标
From:被遍历的数组。
关联数组: 数组名.键名
【foreach.php】
<?php require_once("./smarty.inc.php"); $sy_arr = array('a','b','c','d'); $gl_arr = array('a'=>1001,'b'=>1002,'c'=>1003,'d'=>1004); $data = array(array('uname'=>'tom','age'=>30), array('uname'=>'lily','age'=>100)); $tpl -> assign("sy_arr",$sy_arr); $tpl -> assign("gl_arr",$gl_arr); $tpl -> assign("data",$data); $tpl -> display("foreach.html"); ?>
【foreach.html】
<h1>foreach</h1> {ts:foreach name="f1" item="vo" key="key" from=$sy_arr} {ts:$vo}<br/> {ts:/foreach} <hr> {ts:foreach name="f2" item="vo" key="key" from=$gl_arr} {ts:$vo}<br/> {ts:/foreach} <font color="red">{ts:$gl_arr.b}</font> <hr/> {ts:foreach name="f3" item="vo" key="key" from=$data} {ts:foreach name="f_f3" item="vo2" key="key3" from=$vo} {ts:$vo2} {ts:/foreach} {ts:/foreach} <br/> {ts:foreach name="f3" item="vo" key="key" from=$data} {ts:$vo.uname} {ts:$vo.age} {ts:/foreach}
#在foreach.php中获取前台foreach.html中的值#
Section循环:可以对数据进行二次筛选。
格式:
{ts:section name=”” loop=”” start=”” step=”” max=”” show=””}
{ts:/section}
1,2,3,4,5,6,7,8,9,10
Section:标签名
Name:循环体名字,可以当下标使用,但是不是下标。
Loop:被遍历的数组
Start:遍历数组时,第一个被遍历的数据在数组中的起始位置
正数:从左向右,找第一个元素
负数:从右向左,找第一个元素
Step: 步长
正数: 从左到右
负数:从右向左
Max:筛选的总记录条数
Show:指的是否显示数据 true就是显示数据,false遍历出数据不显示
【section.php】
<?php require_once("./smarty.inc.php"); $sy_arr = array('a','b','c','d'); $gl_arr = array('a'=>1001,'b'=>1002,'c'=>1003,'d'=>1004); $data = array(array('uname'=>'tom','age'=>30), array('uname'=>'lily','age'=>100), array('uname'=>'jack','age'=>10)); $menu = array(array("type"=>"衣服","item"=>array("衬衣","上衣","秋衣")),array("type"=>"鞋子","item"=>array("皮鞋","球鞋","平底鞋")),array("type"=>"家居","item"=>array("床","柜子","沙发"))); $tpl -> assign("sy_arr",$sy_arr); $tpl -> assign("gl_arr",$gl_arr); $tpl -> assign("data",$data); $tpl -> assign("menu",$menu); $tpl -> display("section.html"); ?>
【section.html】
<h4>f1</h4> {ts:section name=f1 loop=$sy_arr} {ts:$sy_arr[f1]} {ts:/section} <h4>f2</h4> {ts:$gl_arr.a}/{ts:$gl_arr.b}/{ts:$gl_arr.c} <h4>f3</h4> {ts:section name="f3" loop=$data} {ts:$smarty.section.f3.index}: {ts:if $smarty.section.f3.first} <font color="red"> {ts:$data[f3].uname}; {ts:$data[f3].age}; </font> {ts:else if $smarty.section.f3.last} <font color="blue"> {ts:$data[f3].uname}; {ts:$data[f3].age}; </font> {ts:else} {ts:$data[f3].uname}; {ts:$data[f3].age}; {ts:/if} <br/> {ts:/section} <h4>f1</h4> {ts:section name=f1 loop=$sy_arr start=-1 step=-1 max=3 show=true} {ts:$sy_arr[f1]} {ts:/section} <hr> <dl> {ts:section name=m1 loop=$menu} <dt>{ts:$menu[m1].type}<dt> {ts:section name="m2" loop=$menu[m1].item} <dd>{ts:$menu[m1].item[m2]}</dd> {ts:/section} {ts:/section} </dl> <dl> {ts:foreach name=m1 item = vo from=$menu} <dt>{ts:$vo.type}<dt> {ts:foreach name="m2" item = v1 from=$vo.item} <dd>{ts:$v1}</dd> {ts:/foreach} {ts:/foreach} </dl>
#在section.php中获取前台section.html中的值#
循环属性:
$smarty.foreach/section.name名字.属性名。
First/last判断是否是最后一个或者第一个 数组元素
Index:当前下标
文件引入:
模板文件引入:模板文件的路径计算是以当前的模板文件为参照进行计算的。
{ts:include file=”路径+文件名”}
php文件引入: 以解析模板的php文件为参照来计算路径的。
{ts:include_php file=”路径+文件名”}
2.0:支持该标签
3.0: 不支持,但是可以使用smartyBC类来代替Smarty类就可以使用了。
前台样式配置文件引入:
用途:配置前台css样式中共性很大的属性值。
配置前台中的固定共性文字信息,图片信息。
{ts:config_load file=”” section=””}
Section: 设置的是调用某块的前台样式配置
前台样式配置:
文件名后缀: .conf
基本语法:
变量名=值
[Chinese]
Title=值
[english]
Title=值
【config.conf】
title=This is my webSite
bgcolor=gray
[chinese]
title=Chinese News
bgcolor=red
[english]
title=English
bgcolor=green
【Include.php】
<?php require_once("./smarty.inc.php"); $tpl -> display("include.html"); ?>
【Include.html】
{ts:config_load file="config.conf"} <hr/> {ts:#title#} <font style="background-color:{ts:$smarty.config.bgcolor}">HELLO</font> <div style="100px;height:100px;background:{ts:#bgcolor#}"> <h4>{ts:#title#}</h4> </div> {ts:config_load file="config.conf" section="chinese"} <div style="100px;height:100px;background:{ts:#bgcolor#}"> <h4>{ts:#title#}</h4> </div> {ts:config_load file="config.conf" section="english"} <div style="100px;height:100px;background:{ts:#bgcolor#}"> <h4>{ts:#title#}</h4> </div>
#在include.php中获取前台include.html和config.conf中的值#