应用Smarty模板

来源:互联网 发布:跳跃网络的游戏 编辑:程序博客网 时间:2024/06/05 06:31

一、所谓Smarty模板引擎,就是将用户界面与PHP程序代码分离开来,彼此分工合作,有效的提高了开发的效率。
二、下载与安装
下载Smarty压缩包,将解压后的文件中libs文件夹放入WWW/Myproject程序目录下,重命名为smarty,再在文件夹中新建四个文件夹,这里写图片描述
三、模板设计
在templates文件夹里新建index.html,

<html>    <head>        <title>{$title}</title>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    </head>    <body>      购书信息:<p>          图书类别:{$arr[0]}<br/>          图书名称:{$arr.name}<br/>          图书单价:{$arr.unit_price.price}/{$arr.unit_price.unit}    </body></html>

在configs文件夹里新建config.php,方便调用

<?phpdefine('BASE_PATH',$_SERVER['DOCUMENT_ROOT']);define('SMARTY_PATH','\Myproject\smarty\\');require BASE_PATH.SMARTY_PATH.'Smarty.class.php';$smarty=new Smarty;$smarty->template_dir=BASE_PATH.SMARTY_PATH.'templates/';$smarty->compile_dir=BASE_PATH.SMARTY_PATH.'templates_c/';$smarty->config_dir=BASE_PATH.SMARTY_PATH.'configs/';$smarty->cache_dir=BASE_PATH.SMARTY_PATH.'cache/';?>

新建一个PHP文件,用来执行

<html><head><meta http-equiv="content-type" content="text/html;charset=utf-8" /></head><?phpinclude'./smarty/configs/config.php';$arr=array('computerbook','name'=>'php从入门到精通',    'unit_price'=>array('price'=>'¥65','unit'=>'本'));$smarty->assign('title','使用Smarty读取数组');$smarty->assign('arr',$arr);$smarty->display("./smarty/templates/index.html");?>     </html>

运行结果

实现以上功能之后,再看看Smarty模板应用所出现的问题:
将smarty文件夹放到项目文件夹10中
这里写图片描述

这里写图片描述

//index.php<?php    include_once "conn/conn.php";    include_once("system/system.inc.php");     $result=mysql_query("select * from tb_book where id order by id limit 3",$conn);    $array=array();                                    while($myrow=mysql_fetch_array($result)){        array_push($array,$myrow);    }    if(!$array){               $smarty->assign("iscommo","F");            }else{        $smarty->assign("iscommo","T");                $smarty->assign("arraybook",$array);    }       $smarty->display('index.html');                ?>
//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><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>将Smarty的配置方法封装到类中</title><link rel="stylesheet" href="../../css/style.css" /></head><body><table width="636" border="1" align="center" cellpadding="1" cellspacing="1"  bgcolor="#FF8B04">  <tr>    <td height="33" align="left" valign="middle" bgcolor="#FFFFFF"><img src="images/shop_14.JPG" width="643" height="33" /></td>  </tr>  <tr>    <td height="132" align="left" valign="middle" bgcolor="#FFFFFF">  {if $iscommo=="T"}      <table width="372"  border="0" cellspacing="0" cellpadding="0">      {section name=bookid loop=$arraybook}          <tr>            <td width="135" rowspan="5" align="center" valign="middle"><img src="{$arraybook[bookid].pics}" width="95" height="100" alt="{$arraybook[bookid].name}" style="border: 1px solid #f0f0f0;" /></td>            <td height="35">图书名称:{$arraybook[bookid].name}</td>        </tr>          <tr>            <td height="23">图书品牌:{$arraybook[bookid].brand}</td>        </tr>          <tr>            <td width="160" height="23">剩余数量:{$arraybook[bookid].stocks}</td>        </tr>          <tr>            <td height="23">市场价:<font color="red">{$arraybook[bookid].m_price}&nbsp;元</font></td>        </tr>          <tr>            <td height="30">会员价格:<font color="#FF0000">{$arraybook[bookid].v_price}&nbsp;元</font></td>        </tr>      {/section}      </table>      <hr style="border: 1px solid #f0f0f0;" />     {/if}     </td>  </tr></table></body></html>
//system.smarty.inc.php<?phpinclude '../smarty/Smarty.class.php';//调用Smarty文件class SmartyProject extends  Smarty{//定义类,继承Smarty父类    function SmartyProject(){//定义方法,配置Smarty模板        $this->template_dir = "../smarty/templates/";//指定模板文件存储在根目录下        $this->compile_dir = "../smarty/templates_c/";//指定编译文件存储在system/smarty/templates_c/文件夹下        $this->config_dir = "../smarty/configs/";        $this->cache_dir = "../smarty/cache/";     }} ?>
//system.inc.php<?phpinclude 'system.smarty.inc.php';//调用类文件$smarty=new SmartyProject();//执行类的实例化操作?>
//conn.php<?php $conn=mysql_connect("localhost","root","root") or die('连接失败:' . mysql_error());mysql_select_db("test",$conn) or die ('数据库选择失败:' . mysql_error());mysql_query("set names utf8");?>
//config.php<?phpdefine('BASE_PATH',$_SERVER['DOCUMENT_ROOT']);//定义服务器的绝对路径define('SMARTY_PATH','\Myproject\10\Smarty\\');//定义Smarty目录的绝对路径require BASE_PATH.SMARTY_PATH.'Smarty.class.php';//加载Smarty类库文件$smarty=new Smarty;//实例化一个Smarty对象//定义各个目录的路径$smarty->template_dir=BASE_PATH.SMARTY_PATH.'templates/';$smarty->compile_dir=BASE_PATH.SMARTY_PATH.'templates_c/';$smarty->config_dir=BASE_PATH.SMARTY_PATH.'configs/';$smarty->cache_dir=BASE_PATH.SMARTY_PATH.'cache/';?>

运行结果:
这里写图片描述

原创粉丝点击