摘要: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定界符内使用系统函数和自定义函数。