smarty笔记

smarty 笔记
display():
把html包含进来
然后用正则匹配php变量
把匹配好的页面重新保存
inclue载入刚才的保存的页面

1.smarty原理
2.smarty安装
3.smarty模板设计
4.smarty程序设计

四个默认文件夹//可在 smarty().__construct改
cache
configs
templates
templates_c

style 和 script的 {} 要注意替换 用left_delimiter("<{")
$s->right_delimiter("}>")
使用模板的时候 smarty就用<{ 代替{
$s->caching = true;//开启缓存

包含图片,css,js要相对于index.php的路径,因为index.php包含执行

./当前目录
../上级目录

基本语法:
注释 {* xxxxx *}

模板中的类对象
$s->assign("obj",new Person());

{config_load "index.conf"} // bgcolor="#ccc";
<body bgcolor="{#bgcolor#}">

操纵php超全局数组
$smarty.get.id <===> $_GET['id'];

//pdo
$pdo = new pdo("mysql:host=localhost;dbname=test","root","root");
$pdo -> setAttribute (PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
$pdo -> setAttribute (PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$pdo ->exec("set names utf8");

//分页
<?php
include ("smarty.inc.php");
include ("pdo.inc.php");

$page = $_GET['page']?$_GET['page']:1;
echo $s->isCached($smarty_dir."/templates/index.html",$page);
if(!$s->isCached($smarty_dir."/templates/index.html",$page))
{
$totsql = "select count(*) from user";
$sth = $pdo -> prepare($totsql);
$sth ->execute();
$tots = $sth -> fetchColumn();


$length = 3;
$offset = ($page - 1) * $length;

$prepage = $page - 1;
$nextpage = $page;
if($nextpage < ceil($tots/$length))
{

$nextpage = $page + 1;
}
else{
$nextpage = $page;
}
$sql = "select * from user order by id limit $offset,$length";
$sth = $pdo->prepare($sql);
$sth -> execute();
$rows = $sth -> fetchALL();
$s -> assign("rows",$rows);
$s -> assign("prepage",$prepage);
$s -> assign("nextpage",$nextpage);
}

$s -> display($smarty_dir."/templates/index.html",$page);
?>

index.html
<body>
<table width="500px" border="3px solid #ccc">
<{foreach $rows as $val}>
<tr>
<td><{$val.id}></td>
<td><{$val.username}></td>
</tr>
<{/foreach}>
</table>
<a href="../index.php?page=<{$prepage}>" title="">上一页</a>|
<a href="../index.php?page=<{$nextpage}>" title="">下一页</a>
</body>

原文地址:https://www.cnblogs.com/Duskcl/p/4875895.html