PHP 关于smarty模板引擎的使用

1、需要下载smarty模板引擎的依赖

利用composer工具查询smarty模板引擎 =>利用composer安装smarty模板引擎

2、smarty 模板引擎的初步的使用

  a、引入smarty的类require_once(''./smarty/smarty/libs/Smarty.class.php)

  b、实例化smarty类

  c、数据绑定

  d、模板替换与展示,具体如下

(php代码部份)

<?php
ini_set('display_errors', true);
header('content-type: text/html; charset=utf8');
require_once('./vendor/smarty/smarty/libs/Smarty.class.php');
require_once('./DB.php');
$res = (new Query('SELECT * FROM `user` WHERE `id`= :id'))->bindValue(':id', 2)->one();
$smarty = new Smarty();
//注意 assign的参数有(key, value, nocache) key可以字符串或者数组, nocache表示是否缓存
$smarty->assign([
    'id' => $res['id'],
    'name' => $res['user'],
    'pwd' => $res['pwd']
], null , false);
try{
    //模板渲染
    $smarty->display('./temp/home.html');
}catch(SmartyException $e){
    exit($e->getMessage());
}catch(Exception $e){
    exit($e->getMessage());
}
?>

(html代码部份)

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>home</title>
</head>
<body>
<div class="container">
    <h1>this is test</h1>
    <table>
        <tr>
            <td>{$id}</td>
            <td>{$name}</td>
            <td>{$pwd}</td>
        </tr>
    </table>
</div>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/rickyctbu/p/11106588.html
今日推荐