PHP Smarty模板引擎使用小結


1、下載

官網github上下載資源包,將資源包中的libs文件夾放置在項目根目錄方便使用

2、smarty的簡單使用

在項目根目錄新建模板文件template.html:

<!DOCTYPE html>
<html>
    <head>
        <title>hello</title>
    </head>
    <body>
        {$str}
    </body>
</html>

代碼解讀:smarty模板引擎的默認左右定界符是左右大括號{},表示里面的內容需要smarty引擎解析,其中$str代表一個變量

在項目根目錄新建php文件index.php:

<?php
//引入smarty核心類
include_once 'libs/Smarty.class.php';
//實例化一個smarty對象
$s = new Smarty();
//分配數據:將'hello world'替換掉$str
$s->assign('str','hello world');
//顯示模板數據
$s->display('template.html');

smarty編譯模板並在項目根目錄的template_c目錄下生成一個編譯后的php文件,

最終會將頁面解析成以下代碼:

<!DOCTYPE html>
<html>
    <head>
        <title>hello</title>
    </head>
    <body>
        hello world
    </body>
</html>

3、smarty簡單配置

$s = new Smarty();

$s->debugging = false;              //是否開啟debug調試,默認關閉
$s->template_dir = 'templates/';    //模板目錄,若該目錄不存在,則默認在當前目錄下尋找模板文件
$s->compile_dir = 'templates_c';    //編譯目錄,不存在則自動創建
$s->config_dir = 'configs/';        //配置項目錄
$s->caching = false;                //是否開啟緩存
$s->cache_dir = 'cache/';           //緩存目錄
$s->cache_lifetime = 3600;          //緩存生命周期(秒)
$s->left_delimiter = '{';           //左定界符
$s->right_delimiter = '}';          //右定界符

4、模板變量

smarty模板中的變量分三類

  • PHP分配變量,在assign方法分配的變量,assign一般會分配三種數據類型給模板使用
    • 標量數據:使用$變量名訪問
    • 數組數據:使用$變量名.下標或$變量名[下標]訪問
    • 對象數據:使用$變量名->屬性名訪問
      • PHP代碼
        include_once 'libs/Smarty.class.php';
        $s = new Smarty();
        //標量分配
        $s->assign('str','hello world');
        //數組分配
        $s->assign('arr',array(1,2,3));
        class Person{
            public $name = 'testobj';
            public $age = 12;
        }
        //對象分配
        $s->assign('obj',new Person());
        $s->display('template.html');
      • 模板代碼
        <!DOCTYPE html>
        <html>
            <head>
                <title>hello</title>
            </head>
            <body>
                {*注釋:使用標量數據*}
                {$str}
                {*注釋:使用數組數據*}
                {$arr.0}----{$arr[1]}
                {*注釋:使用對象數據*}
                {$obj->name}----{$obj->age}
            </body>
        </html>
      • 頁面最終結果代碼
        <!DOCTYPE html>
        <html>
            <head>
                <title>hello</title>
            </head>
            <body>
                        hello world
                        1----2
                        testobj----12
            </body>
        </html>
  • smarty保留變量,主要引用一些常用系統變量和smarty內部變量
    • GET數據:{$smart.get.下標}
    • POST數據:{$smart.post.下標}
    • Session數據:{$smart.session.下標}
    • Cookie數據:{$smart.cookie.下標}
    • REQUEST數據:{$smart.request.下標}
    • Server數據:{$smart.server.大寫下標}
    • 時間戳:{$smart.now}
    • 模板路徑:{$smart.current_dir}
    • 模板名字:{$smart.template}
    • 配置文件:{$smart.config.下標}
  • 自定義變量:在模板中定義變量並使用
    • 語法:{assign var='變量名' value='變量值'}
    • 例:
      <!DOCTYPE html>
      <html>
          <head>
              <title>hello</title>
          </head>
          <body>
              {*注釋:使用標量數據*}
              {$str}
              {*注釋:使用數組數據*}
              {$arr.0}----{$arr[1]}
              {*注釋:使用對象數據*}
              {$obj->name}----{$obj->age}
              {*聲明自定義變量*}
              {assign var='num' value='10086'}
              {*使用自定義變量*}
              {$num}
          </body>
      </html>

5、模板配置文件

配置文件格式:可以直接使用txt文件

配置項格式:配置名稱 = 值

使用#代表注釋,默認所有配置項都是全局的,[局部]代表局部訪問

#注釋:全局配置
bgColor = #00ff00
#局部配置
[customer]
title = 'customer_title'

在模板文件中加載配置文件:

<!DOCTYPE html>
<html>
    <head>
        {*配置文件加載*}
        {config_load file='smarty_config/config.txt'}
        {*使用$smarty.config.配置名稱訪問,這里無法訪問局部變量*}
        <title>{$smarty.config.title}</title>
    </head>
    {*使用#配置名稱#訪問配置項*}
    <body bgcolor="{#bgColor#}"></body>
</html>

6、模板流程控制語法結構

  • 判斷:
{if 判斷語句1}
分支1
{elseif 判斷語句2}
分支2
{else}
分支3
{/if}  結束
  • 循環:
{*形式1:原始foreach模式*}
{foreach $arr as $var}
    循環體
{/foreach}  結束
{*形式2:smarty專屬模式*}
{foreach from=$arr key='鍵名' item='值名'}
    循環體
{/foreach}  結束
{*使用以下方式也可以取得鍵名*}
{$值名@key}

smarty foreach循環可訪問屬性,使用$循環變量名@屬性訪問

屬性名 說明
index 當前數組索引(循環內部使用)
iteration 當前循環次數(循環內部使用)
first 首次循環(循環內部使用)
last 最后一次循環(循環內部使用)
show 循環是否執行判定:true表示有數據,false表示沒數據(循環內外部都可以使用)
total foreach執行總次數(循環內外部都可以使用)
{assign var='p' value=array('name'=>'hzc','age'=>20,'sex'=>1)}
{if is_array($p)}
    {foreach $p as $v1}
        {$v1@key}=>{$v1}
    {/foreach}
    {foreach $p as $k2=>$v2}
        {$k2}=>{$v2}
    {/foreach}
    {foreach from=$p key='k3' item='v3'}
        第{$v3@iteration}次循環:{$v3@index}:{$v3@key}=>{$v3}
    {/foreach}
    {if $v3@show}一共循環了{$v3@total}次{/if}
{/if}

7、文件包含

 

{include file='filename'}

index.html

<!DOCTYPE html>
<html>
    <head></head>
    <body>
        {include file='arr.tpl'}
    </body>
</html>

arr.tpl

{assign var='p' value=array('name'=>'hzc','age'=>20,'sex'=>1)}
{if is_array($p)}
    {foreach from=$p key='k' item='v'}
        第{$v@iteration}次循環:{$k}=>{$v}
    {/foreach}
{/if}

8、前端語法保護

smarty不會解析{literal}和{/literal}之間的代碼,可以防止誤解析

9、使用外部函數

smarty支持在定界符內直接使用系統函數和用戶自定義函數,只要保證函數在內存中即可

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM