首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

Smarty 模板引擎入門:變量賦值與模板中訪問完整指南

     編輯:PHP 相關知識

前言

Smarty 是 PHP 經典模板引擎,核心思想是「邏輯與顯示分離」。本文通過完整範例展示:初始化配置、控制器賦值、模板中訪問各類變量(標量、索引陣列、關聯陣列、多維陣列、物件)、變量運算與修飾符。

1. 初始化配置:init.inc.php

<?php
define('ROOT_PATH', dirname(__FILE__));
require ROOT_PATH . '/libs/Smarty.class.php';

$tpl = new Smarty();
$tpl->template_dir = ROOT_PATH . '/tpl/';
$tpl->compile_dir  = ROOT_PATH . '/com/';
$tpl->left_delimiter  = '<{';  // 自訂左定界符,避免與 JS 模板衝突
$tpl->right_delimiter = '}>'; // 自訂右定界符
?>

2. 控制器:index.php

<?php
require 'init.inc.php';
require 'Person.class.php'; // 示範物件賦值

// 標量
$tpl->assign('title', 'Smarty 變量示範');
$tpl->assign('content', '這是模板內容區塊');

// 索引陣列
$tpl->assign('arr1', ['abc', 'def', 'ghi']);
$tpl->assign('arr2', [['abc','def','ghi'], ['jkl','mno','pqr']]); // 二維

// 關聯陣列
$tpl->assign('arr3', ['one'=>'111', 'two'=>'222', 'three'=>'333']);
$tpl->assign('arr4', [
    'one' => ['one'=>'111', 'two'=>'222'],
    'two' => ['three'=>'333', 'four'=>'444']
]); // 二維關聯

// 混合陣列
$tpl->assign('arr5', [
    'one' => ['111', '222'],
    ['three'=>'333', 'four'=>'444']
]);

// 物件
$tpl->assign('person', new Person('小易', 10));

// 數值運算示範
$tpl->assign('num1', 10);
$tpl->assign('num2', 20);

$tpl->display('index.tpl');
?>

3. 物件類別:Person.class.php

<?php
class Person {
    public $name;
    public $age;

    public function __construct($name, $age) {
        $this->name = $name;
        $this->age  = $age;
    }

    public function hello() {
        return "您好!我叫{$this->name},今年{$this->age}歲了。";
    }
}
?>

4. 模板:index.tpl

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title><{$title}></title>
</head>
<body>
    <h1>變量訪問示範</h1>

    <p>標量:<{$content}></p>

    <p>索引陣列:<{$arr1[0]}> <{$arr1[1]}> <{$arr1[2]}></p>

    <p>二維索引陣列:
        <{$arr2[0][0]}> <{$arr2[0][1]}> <{$arr2[0][2]}>
        <{$arr2[1][0]}> <{$arr2[1][1]}> <{$arr2[1][2]}>
    </p>

    <p>關聯陣列:<{$arr3.one}> <{$arr3.two}> <{$arr3.three}></p>

    <p>二維關聯陣列:
        <{$arr4.one.one}> <{$arr4.one.two}>
        <{$arr4.two.three}> <{$arr4.two.four}>
    </p>

    <p>混合陣列:
        <{$arr5.one[0]}> <{$arr5.one[1]}>
        <{$arr5[0].three}> <{$arr5[0].four}>
    </p>

    <p>物件屬性:<{$person->name}> <{$person->age}></p>
    <p>物件方法:<{$person->hello()}></p>

    <p>變量運算:<{$num1+$num2}> ( = 30 )</p>
    <p>混合運算:<{$num1+$num2*$num2/$num1+44}> ( = 94 )</p>
</body>
</html>

5. 執行結果

標量:這是模板內容區塊
索引陣列:abc def ghi
二維索引陣列:abc def ghi jkl mno pqr
關聯陣列:111 222 333
二維關聯陣列:111 222 333 444
混合陣列:111 222 333 444
物件屬性:小易 10
物件方法:您好!我叫小易,今年10歲了。
變量運算:30
混合運算:94

關鍵語法總結

PHP 賦值類型模板訪問語法備註
標量<{$var}>直接輸出
索引陣列<{$arr[0]}>數字索引
關聯陣列<{$arr.key}>點號訪問鍵名
多維陣列<{$arr.0.key}> 或 <{$arr[key1][key2]}>混合使用點號與括號
物件屬性<{$obj->prop}>箭頭操作符
物件方法<{$obj->method()}>可傳參:<{$obj->method($arg)}>
運算表達式<{$a+$b*$c}>支援 + - * / % 等

進階提示

  • 修飾符:<{$var|escape:'html'}>、<{$var|date_format:'%Y-%m-%d'}>、<{$var|truncate:30}>。
  • 條件判斷:<{if $var}>...<{else}>...<{/if}>。
  • 迴圈:<{foreach $arr as $k=>$v}>...<{/foreach}>。
  • 快取:$tpl->caching = true; $tpl->cache_lifetime = 3600; 啟用模板快取。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved