smarty模板

原创 2018-12-18 17:00:29 271
摘要:php文件::: <?php //加载smarty模板引擎 require __DIR__ . '/vendor/autoload.php'; //创建smarty对象 $smarty = new Smarty(); //配置目录 $smarty->setCacheDir(__DIR__ .&nb
php文件:::
<?php
//加载smarty模板引擎
require __DIR__ . '/vendor/autoload.php';

//创建smarty对象
$smarty = new Smarty();

//配置目录
$smarty->setCacheDir(__DIR__ . '/catch');//设置缓存目录
$smarty->setConfigDir(__DIR__ . '/config');//设置配置目录
$smarty->setCompileDir(__DIR__ . '/tmp_c');//设置编译目录
$smarty->setTemplateDir(__DIR__ . '/temp');//设置模板目录

//设置定界符
//$smarty->setRightDelimiter('}>');
//$smarty->setLeftDelimiter('<{');

//配置缓存
//$smarty->setCaching(false);//设置缓存是否开启
//$smarty->setCacheLifetime(60*60*24*7);//设置缓存有效期

$conn = new PDO('mysql:host=127.0.0.1;dbname=zhuchang','root','');
$stmt = $conn->prepare('select * from ims_users limit 100,20');
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
//$res = [];
$smarty->assign('res',$res);//变量赋值
$smarty->display('test1.html');//模板渲染
html文件:::
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table align="center" width="80%" style="text-align: center">
    <thead>
    <tr>
        <th>用户名</th>
        <th>注册时间</th>
        <th>最后登录时间</th>
        <th>最后登录IP</th>
        <th>状态</th>
    </tr>
    </thead>
    <tbody>
{foreach $res as $k=>$v}
    <tr>
        <td>{$v.username}</td>
        <td>{$v.joindate|date_format:"%Y-%m-%d %H:%M:%S"}</td>
        <td>{$v.lastvisit|date_format:"%Y-%m-%d %H:%M:%S"}</td>
        <td>{$v.lastip}</td>
        <td>{$v.status}</td>
    </tr>
{foreachelse}
<tr>
    <td><h1>没有数据</h1></td>
</tr>
{/foreach}
    </tbody>
</table>
</body>
</html>

在smarty模板中使用变量之前要先分配变量,才能使用。模板需要渲染之后才可以使用。

smarty中还可以使用类的访问方法,类对象需要提前分配才可以使用。

可以直接在smarty定界符内使用系统函数和自定义函数。

显示常量时使用$smarty.const.SITENAME,SITENAME常量可以不用assign分配。

在获取显示配置文件时,要先使用{config_load file="配置文件名"}加载配置文件,然后使用{$smarty.config.配置字段名}

批改老师:天蓬老师批改时间:2018-12-18 18:05:28
老师总结:Smarty模板引擎的设计思想, 在那个模板引擎还流行的年代, 影响了一代php程序员, 也影响了一大批同类的软件 , 值得学习

发布手记

热门词条