• 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>
    
  • 相关阅读:
    ServletWebServerApplicationContext -带有*WebxxxApplication的容器
    SpringMvc-DispatchServlet初始化
    SimpleDateFormat和java8日期格式化
    01导入配置类和用户自定义添加db。ImportBeanDefinitionRegistrar和DeferredImportSelector
    @ConfigurationProperties和@PropertySource
    mybatis语句的存储
    leetCode3: 无重复字符的最长子串
    八锁问题
    数据库杂记
    背包问题
  • 原文地址:https://www.cnblogs.com/lzzhuany/p/4836416.html
Copyright © 2020-2023  润新知