Smarty中一些标签的使用

Smarty中的标签和php中的标签不一样

foreach标签{foreach   from=$goods(变量名) key='键,不带$' item='值,不带$'}中间的显示内容{/foreach}

section标签{section loop=$goods变量名 name=临时角标} 显示内容$goods[临时角标].goods_id{/section}

while循环标签{while $age<18}中间是显示内容,$age要进行运算,否则将是死循环{/while}

封装类mySmarty.php

<?php
//将smarty的配置信息封装成类
class mySmarty extends Smarty{
	public function __construct(){
		parent::__construct();//首先调用父类的构造函数

		$this->template_dir='./temp';//显示文件的目录
		$this->compile_dir='./comp';//编译好的文件目录
	}
}

?>

 控制页面01.php

<?php
//引入Smarty类
require('../../smarty3/libs/Smarty.class.php');

//引入封装的子类
require('./mySmarty.php');

$mysm=new mySmarty();

$conn=mysql_connect('localhost','root','111111');
mysql_query('use boolshop',$conn);//选库
mysql_query('set names utf8',$conn);//设置编码
$sql='select goods_id,goods_name,goods_number,shop_price from goods limit 5';
$rs=mysql_query($sql,$conn);
$arr=array();
while(($row=mysql_fetch_assoc($rs))!==false){ 
    $arr[]=$row;
}
//print_r($arr);
$mysm->assign('goods',$arr);

//定义一个年龄属性
$mysm->assign('age',16);
//传当前时间的时间戳
$mysm->assign('time',time());
$mysm->display('01.html');
?>

 01.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>显示数据</title>
    <style type="text/css">
{literal}
p{
    font-size:25px;
    background:green;
}
    
{/literal}
    </style>

</head>
<body>
用foreach遍历数组<br/>
{foreach from=$goods key='key' item='v'}
    <p>
     id号:{$v['goods_id']},商品名字:{$v['goods_name']},商品价格{$v['shop_price']},商品储存量   {$v['goods_number']}

    </p>
{/foreach}
用section遍历数组<br/>
{section loop=$goods name=g}
    <p>
    id号:{$goods[g]['goods_id']},商品名字:{$goods[g]['goods_name']},商品价格{$goods[g]['shop_price']},商品储存量   {$goods[g]['goods_number']}
    </p>
{/section}
使用while循环<br/>
{while $age<18}
    你{$age++}岁了,还没有成年。
{/while}

格式化时间戳<br/>
当前时间是{$time|date_format:'%Y-%m-%d  %H:%M:%S'}
</body>
</html>

 

转载于:https://www.cnblogs.com/lzzhuany/p/4836416.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值