欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

smarty模板引擎基礎(chǔ)知識(shí)入門(mén)

 更新時(shí)間:2015年03月30日 09:41:40   作者:詩(shī)未冷  
這篇文章主要介紹了smarty模板引擎基礎(chǔ)知識(shí)入門(mén),較為詳細(xì)的分析了smarty的基本概念并實(shí)例分析了相關(guān)的基本用法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了smarty模板引擎基礎(chǔ)知識(shí)。分享給大家供大家參考。具體如下:

一、基本概念

1.什么是mvc?
mvc是一種開(kāi)發(fā)模式,核心思想是:數(shù)據(jù)的輸入、數(shù)據(jù)的處理、數(shù)據(jù)顯示的強(qiáng)制分離。
2.什么是smarty?
smarty是一個(gè)php的模板引擎。更明確的來(lái)說(shuō),它可以幫助開(kāi)發(fā)者更好的分離程序邏輯和頁(yè)面顯示。

3.smarty運(yùn)行原理
模板文件,就是一個(gè)顯示數(shù)據(jù)的模板,其中需要顯示的數(shù)據(jù)用占位符代替。
smarty運(yùn)行時(shí),會(huì)讀取模板文件,將模板文件中的占位符替換成真正的數(shù)據(jù),并輸出一個(gè)處理后的php文件,交由服務(wù)器運(yùn)行。

二、自己寫(xiě)一個(gè)smarty模板

為了更好的理解smarty模板,現(xiàn)在自己先寫(xiě)一個(gè)自己的smarty模板-minismarty,讓自己更加深入的了解smarty運(yùn)行原理。

1.新建項(xiàng)目minismarty
新建模板文件路徑:templates
新建模板文件被編譯后的文件路徑:templates_c
新建模板文件:intro.tpl
新建運(yùn)行的文件:index.php
新建自己的smarty,即處理模板的文件:cls_MiniSmarty.php

2.編寫(xiě)index.php文件

<?php 
  require_once './cls_MiniSmarty.php';
  $miniSmarty = new MiniSmarty();
  //傳遞數(shù)據(jù)
  $miniSmarty->assign("title","hello minismarty!");
  $miniSmarty->assign("content","<font color='red'>this is content!</font>");
  //傳遞數(shù)據(jù)到哪個(gè)頁(yè)面顯示
  $miniSmarty->display("intro.tpl");
?>

3.編寫(xiě)intro.tpl文件

<!--這是個(gè)模板文件--> 
<html> 
<head> 
<meta http-equiv="Content-Language" content="en" />
<meta name="GENERATOR" content="PHPEclipse 1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>{$title}</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#FF9966" vlink="#FF9966" alink="#FFCC99">
{$content}
</body>
</html>

這里面的內(nèi)容是用占位符的形式,smarty的作用就是將占位符的內(nèi)容替換成真正的數(shù)據(jù)。
這樣就可以實(shí)現(xiàn)模板文件和數(shù)據(jù)文件強(qiáng)制分離,通過(guò)smarty進(jìn)行數(shù)據(jù)的傳遞。

4.編寫(xiě)cls_MiniSmarty.php文件

<?php 
/** 
 * 
 * 原本是通過(guò)smarty模板引擎給模板提供數(shù)據(jù)的 
 * 現(xiàn)在自己模仿寫(xiě)一個(gè)模板,給模板提供數(shù)據(jù)的類 
 * smarty運(yùn)行時(shí),讀取模板文件,將模板文件替換成可運(yùn)行的php文件
 * 服務(wù)器真正運(yùn)行的文件是處理后的文件
 */ 
class MiniSmarty { 
  //模板文件路徑 
  var $template_dir = "./templates/"; 
  //模板文件被替換后的文件路徑 
  var $templates_c_dir = "./templates_c/"; 
  //存放變量值 
  var $tpl_vars = array (); 
  //主要模擬2個(gè)方法 
  /** 
   * 添加數(shù)據(jù) 
   * 參數(shù)1:鍵 
   * 參數(shù)2:值,默認(rèn)為null 
   */ 
  function assign($tpl_var, $var = null) { 
    if ($tpl_var != '') { 
      $this->tpl_vars[$tpl_var] = $var; //將數(shù)據(jù)添加到數(shù)組中 
    } 
  } 
  /** 
   * 顯示數(shù)據(jù) 
   * 參數(shù)1:顯示到哪個(gè)模板文件中 
   */ 
  function display($tpl_file) { 
    //獲得模板文件的路徑 
    $tpl_file_path = $this->template_dir . $tpl_file; 
    //獲得模板文件被編譯后的文件路徑 
    $compile_file_path = $this->templates_c_dir . "com_" . $tpl_file . ".php"; 
    //判斷文件是否存在 
    if (!file_exists($tpl_file_path)) { 
      return false; 
    } 
    //不用每次都生成編譯文件,只有編譯文件不存在或者模板文件被修改了才生成新的編譯文件 
    //相當(dāng)于緩存了編譯文件 
    //filemtime函數(shù):獲得文件的生成時(shí)間 
    if (!file_exists($compile_file_path) || filemtime($tpl_file_path) > filemtime($compile_file_path)) { 
      //讀取模板文件的內(nèi)容 
      $fpl_file_content = file_get_contents($tpl_file_path); 
      $newStr = myReplace($fpl_file_content); 
      //將替換后的字符串生成新的文件,也就是編譯后的文件 
      file_put_contents($compile_file_path, $newStr); 
    } 
    //引入編譯后的文件 
    include $compile_file_path; 
  } 
  /** 
   * 對(duì)模板文件中的內(nèi)容進(jìn)行替換,獲得新的字符串 
   */ 
  function myReplace($fpl_file_content) { 
    $pattern = array ( 
      '/\{\s*\$([a-zA-Z_][a-zA-Z0-9_]*)\s*\}/i' 
    ); 
    $replace = array ( 
      '<?php echo $this->tpl_vars["${1}"] ?>' 
    ); 
    $newStr = preg_replace($pattern, $replace, $fpl_file_content); 
    return $newStr; 
  } 
} 
?>

preg_replace方法介紹:
參數(shù)1:替換的規(guī)則
參數(shù)2:替換成的內(nèi)容
參數(shù)3:替換操作的內(nèi)容

5.運(yùn)行結(jié)果

標(biāo)題和內(nèi)容都顯示出來(lái)了:

結(jié)論:

真正運(yùn)行的文件,既不是index.php,也不是intro.php,而是二者通過(guò)smarty作用后的文件:
com_intro.tpl.php。這個(gè)文件中數(shù)據(jù)來(lái)源于index.php,顯示的布局來(lái)自intro.tpl,中間的橋梁是smarty。
smarty的作用是接受數(shù)據(jù)、填充數(shù)據(jù)(替換模板中的占位符)、并加載替換后的文件。

三、講解smarty使用細(xì)節(jié)

1.如何配置smarty?

解壓后,將libs文件夾拷貝到項(xiàng)目目錄下即可,然后再創(chuàng)建2個(gè)文件夾templates和templates_c,分別放模板文件和模板編譯后文件。

2.使用smarty注意事項(xiàng)

①替換變量的標(biāo)示符。
因?yàn)槟J(rèn)的表示符是{}這個(gè)和style樣式中的{}發(fā)生沖突,所以需要修改一下默認(rèn)的標(biāo)識(shí)符,一般修改為:{<>}
②修改標(biāo)識(shí)符的方法。
方法一:直接修改smarty類源碼:不推薦。
方法二:使用smarty提供的方法進(jìn)行修改。

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

③smarty的一些基本配置

$smarty->template_dir="./templates";//模板路徑
$smarty->compile_dir="./templates_c";//編譯路徑
$smarty->caching=false;  //是否使用緩存
$smarty->cache_dir="./smarty_cache";//如果使用緩存的話:緩存的路徑

3.smarty模板技術(shù)分配變量的細(xì)節(jié)問(wèn)題

一句話:可以分配php支持的各種數(shù)據(jù)。
php基本數(shù)據(jù):int double string bool
復(fù)合數(shù)據(jù)類型:array object
特殊數(shù)據(jù)類型:resource null

希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論