php smarty基本使用

1.基本类

//smarty_config.php
<?php
define('TEMPLATE_DIR','templates/');
define('COMPILE_DIR','templates_c/');
define('CONFIG_DIR','configs/');
define('CACHE_DIR','cache/');

?>
//View.class.php
<?php
//配置文件
require_once 'configs/smart_config.php';
//Smarty类
require('smarty/Smarty.class.php');
class View extends Smarty {
function SmartyFactory() {
$this->Smarty();
//模板路径
$this->template_dir = TEMPLATE_DIR;
//编译后文件
$this->compile_dir = COMPILE_DIR;
//配置文件
$this->config_dir = CONFIG_DIR;
//缓存文件
$this->cache_dir = CACHE_DIR;
$this->debugging = true;
}
//重新封装display 个人习惯改成show
function show($name,$cacheId='') {
if($cacheId == '')
$this->display($name.'.htm');
else
$this->display($name.'.htm',$cacheId);
}
//重新封装assign 习惯用add
function add($name,$value) {
$this->assign($name,$value);
}
}
?>

[color=red]
基本目录如:
test
|-configs
|-templates
|-templates_c
|-cache
|-smarty --smarty lib[/color]
2.基本使用

//index.php
<?php
require_once('View.php');
//数据查询 mysql_fetch_assoc() 返回数组
//数据库层封装
//如:$User = new User();
//$Users = $User->paging('参数');
$View = new View();
$View->add("users",$Users);
$View->show("index");
?>

[color=red]//public.conf
[public]
public = public/
img = public/img/
css = public/css/
js = public/js/[/color]

//index.htm --在templates文件夹下 简短介绍下
<html>
<!-- 包含meta.htm文件-->
{include file="meta.htm"}
<!-- 利用smarty config文件 定义基本信息-->
{config_load file=public.conf section="public"}
<!-- 略 -->
<table>
<tr>
<!-- {#img#} img的基本路径 对于一般小网站的做法 如有是使用了二级域名的静态服务器没必要了-->
<td><input type="image" src="{#img#}xxx.gif"/></td>
</tr>
</table>
<table>
<tr>
<td>id</td><td>用户名</td>
</tr>
<!-- 循环遍历 数组里的数据 -->
<!-- loop=$Users 在php中的变量 name=user循环的名称-->
{section name=user loop=$Users}
<tr>
<td>{$Users[user].id}</td>
<td>{$Users[user].username}</td>
</tr>
{/section}
</table>
<!-- 包含另一个php 这个php可以是index.php这样的模板输出-->
{php}
include("other.php");
{/php}
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值