PHP之Smarty模版簡單應用

來源:互聯網
上載者:User

什麼是模版,我的理解是將介面和實現代碼分離開來,

這樣做的目的將美工和程式員的工作分離開來,提高工作效率,方便後期維護。

PHP也有比較多的模版,例如PHPLIB Template、FastTemplate、Smarty等

今天我研究的是Smarty

到Smarty的官方網站下載最新的Smarty套件: http://smarty.php.net/
解開 Smarty 2.6.0 後,會看到很多檔案,其中有個 libs 資料夾。在 libs 中應該會有 3 個 class.php 檔 + 1 個 debug.tpl + 1 個 plugin 資料夾 + 1 個 core 資料夾。然後直接將 libs 複製到您的程式主資料夾下

如上項目結構         那個libs就是Smart的內庫

common.php        增強可重複性建立此  因為很多頁面都會用到如下設定,所以把他提取出來,每次只要引用即可!

<?php
require('smarty/mysql.class.php');
$dbcharset = 'utf8';
$query = new dbQuery('localhost', 'root', 'sa','demo');

require 'smarty/libs/Smarty.class.php';

// 初始化smarty
$smarty = new Smarty;

$smarty->compile_check = true; // 開啟編譯檢查
$smarty->debugging = false;

$smarty->template_dir   = 'templates/'; //模板目錄  可自訂 只要可在項目中確實存在此目錄
$smarty->compile_dir    = 'templates_c/';//編譯檔案目錄  同上

$smarty->left_delimiter  = '<!--{';//左標記  解析模版頁中的特定代碼塊   可自訂  
$smarty->right_delimiter  = '}-->';//右標記

?>

 mysql.class.php

<?php
/**
 * mysql查詢類
 *
 */
class dbQuery {
 /**
  * 查詢總次數
  *
  * @var int
  */
 var $querynum = 0;
 /**
  * 串連控制代碼
  *
  * @var object
  */
 var $link;
 
 /**
  * 建構函式
  *
  * @param string $dbhost 主機名稱
  * @param string $dbuser 使用者
  * @param string $dbpw   密碼
  * @param string $dbname 資料庫名
  * @param int $pconnect 是否持續串連
  */
 function dbQuery($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0) {
  if($pconnect) {
   if(!$this->link = @mysql_pconnect($dbhost, $dbuser, $dbpw)) {
    $this->halt('Can not connect to MySQL server');
   }
  } else {
   if(!$this->link = @mysql_connect($dbhost, $dbuser, $dbpw)) {
    $this->halt('Can not connect to MySQL server');
   }
  }
  if($this->version() > '4.1') {
   global $dbcharset;
   if($dbcharset) {
    mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary", $this->link);
   }

   if($this->version() > '5.0.1') {
    mysql_query("SET sql_mode=''", $this->link);
   }
  }

  if($dbname) {
   mysql_select_db($dbname, $this->link);
  }

 }
 /**
  * 選擇資料庫
  *
  * @param string $dbname
  * @return
  */
 function select_db($dbname) {
  return mysql_select_db($dbname, $this->link);
 }
 /**
  * 取出結果集中一條記錄
  *
  * @param object $query
  * @param int $result_type
  * @return array
  */
 function fetch_array($query, $result_type = MYSQL_ASSOC) {
  return mysql_fetch_array($query, $result_type);
 }
 
 /**
  * 查詢SQL
  *
  * @param string $sql
  * @param string $type
  * @return object
  */
 function query($sql, $type = '') {
  
  $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
   'mysql_unbuffered_query' : 'mysql_query';
  if(!($query = $func($sql, $this->link)) && $type != 'SILENT') {
   $this->halt('MySQL Query Error', $sql);
  }

  $this->querynum++;
  return $query;
 }
 /**
  * 取影響條數
  *
  * @return int
  */
 function affected_rows() {
  return mysql_affected_rows($this->link);
 }
 /**
  * 返回錯誤資訊
  *
  * @return array
  */
 function error() {
  return (($this->link) ? mysql_error($this->link) : mysql_error());
 }
 /**
  * 返回錯誤碼
  *
  * @return int
  */
 function errno() {
  return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
 }
 /**
  * 返回查詢結果
  *
  * @param object $query
  * @param string $row
  * @return mixed
  */
 function result($query, $row) {
  $query = @mysql_result($query, $row);
  return $query;
 }
 /**
  * 結果條數
  *
  * @param object $query
  * @return int
  */
 function num_rows($query) {
  $query = mysql_num_rows($query);
  return $query;
 }
 /**
  * 取欄位總數
  *
  * @param object $query
  * @return int
  */
 function num_fields($query) {
  return mysql_num_fields($query);
 }
 /**
  * 釋放結果集
  *
  * @param object $query
  * @return bool
  */
 function free_result($query) {
  return mysql_free_result($query);
 }
 /**
  * 返回自增ID
  *
  * @return int
  */
 function insert_id() {
  return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
 }
 /**
  * 從結果集中取得一行作為枚舉數組
  *
  * @param object $query
  * @return array
  */
 function fetch_row($query) {
  $query = mysql_fetch_row($query);
  return $query;
 }
 /**
  * 從結果集中取得列資訊並作為對象返回
  *
  * @param object $query
  * @return object
  */
 function fetch_fields($query) {
  return mysql_fetch_field($query);
 }
 /**
  * 返回mysql版本
  *
  * @return string
  */
 function version() {
  return mysql_get_server_info($this->link);
 }
 /**
  * 關閉串連
  *
  * @return bool
  */
 function close() {
  return mysql_close($this->link);
 }
 /**
  * 輸出錯誤資訊
  *
  * @param string $message
  * @param string $sql
  */
 function halt($message = '', $sql = '') {
  echo $message . ' ' . $sql;
  exit;

 }
}

?>

 

userList.php

<?php
include_once 'common.php';
$result=$query->query("select * from userinfo");
$userList=array();
while ($row=$query->fetch_array($result))
{
 array_push($userList,$row);
}
$query->free_result($result);
$query->close();
$smarty->assign('userList',$userList);//給模板賦值
$smarty->display('userList.html');//顯示模板內容
?>

 

userList.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用者列表</title>
</head>
<body>
<center>
<table>

<tr><th>使用者名稱</th><th>密碼</th><th>年齡</th><th>備忘</th></tr>
<!--{foreach  from=$userList item=user}-->
<tr>
<td><!--{$user.userName}--></td>
<td><!--{$user.pwd}--></td>
<td><!--{$user.age}--></td>
<td><!--{$user.remark}--></td>
</tr>
<!--{/foreach}-->
</table>
</center>
</body>
</html>

 

 

運行userList.php

使用者名稱 密碼 年齡 備忘
zhangsan 12345 20 aa
liudehua gggggg 40 aa

 

 

測試成功! 個人小結  那模版頁面就好比是含有多個Smart標記的一個預留位置,然後進行動態操作,我只是一個小小DEMO,但感覺這樣的方式是挺不錯的 一定程度上解耦合了,呵呵  還要繼續學習啊 !

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.