模板引擎(smarty)知识点总结五

---------重点知识:循环------------

 /*
   smarty 循环之for循环
 */

  /*
    基本的语法
        {for $i=$start to $end step = 1}
            表示从$start开始循环 再到$end结束  step 表示步长
        {/for}
  */

  $msma->assign('start',1);
  $msma->assign('end',100);
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> smarty11 </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
 </head>

 <body>
    <pre>
  {literal}
模板的循环 {for $i=$start to $end step 1}{/for} 表示从$start开始循环 再到$end结束 step 表示步长 将第一行与最后一行的内容颜色变红 {if $i@first == $i@iteration} {else if $i@last == $i@iteration} 也可以这样 {if$i@first} {if@last} 因为源码中是只要是first和last就为真其余都为0
{/literal}
</pre> <P> {for $i=$start to $end} {$i}<br/> {/for} </p> <h2>每3个换一行</h2> <P> {for $i=$start to $end} {$i}&nbsp;{if ($i%3 == 0)}<br/>{/if} {/for} </p> <p> 输出奇数 {for $i=$start to $end step 2} {$i}<br/> {/for} </p> <h2>每3个换一行</h2> <p> {for $i=$start to $end step 2} {$i}&nbsp; {if $i@iteration %3 == 0}<br/>{/if} {/for} <h2>一共{$i@total}行</h2> </p> <h2>将第一行与最后一行的内容颜色变红</h2> <p> {for $i=$start to $end step 5} {if $i@first == $i@iteration} <font color='red'>{$i}&nbsp;</font> {else if $i@last == $i@iteration} <font color='red'>{$i}&nbsp;</font> {else} {$i}&nbsp; {/if} {/for} <h2>一共{$i@total}行</h2> </p> </body> </html>

----foreach循环

  /*
    基本的语法
        smarty2的写法---{foreach from=循环的数组  key=k item=item}{/foreach}
            smarty3的写法---{foreach $数组 as $k=>$v}{/foreach}
  */

<?php
 /************
   YJC php 之路
 ************/
 /*
   smarty 循环之foreach循环
 */
 ##########
 header('content-type:text/html;charset=utf-8');
  require_once 'libs/Smarty.class.php';
  require 'MySmarty.class.php';
  $msma = new MySmarty();
  /*
    基本的语法 
        smarty2的写法---{foreach from=循环的数组  key=k item=item}{/foreach}
            smarty3的写法---{foreach $数组 as $k=>$v}{/foreach}
  */
  $conn = new mysqli('localhost','root','root','boolshop');
  $conn->query('set names utf8');
  if($conn->connect_error){
    die($conn->connect_error);
  }
  $sql = 'select * from goods limit 10';
  $res = $conn->query($sql);
  $data = array();
  while($row = $res->fetch_assoc()){
    $data[]  = $row;
  }

  $msma->assign('goodslist',$data);
  $msma->display('temp12.html');
?>
<!DOCTYPE html>
<html>
 <head>
  <title> smarty12 </title>
  <meta charset='utf-8'/>
 </head>

 <body>
   {literal}
    <pre>
        模板的循环 
            smarty2的写法---{foreach from=循环的数组  key=k item=item}/foreach}
            smarty3的写法---{foreach $数组 as $k=>$v}{/foreach}
    </pre>
    {/literal}
        <table height='400' cellspacing='0' cellpadding='0' border='1'>
            <tr>
                <th>编号</th>
                <th>商品序号</th>
                <th>商品名</th>
                <th>商品价格</th>
            </tr>
            {foreach key=key item=item from=$goodslist}
            {if $item@first || $item@last}
            <tr style='background-color:#ccc'>
                <td>{$item.goods_id}</td>
                <td>{$item.goods_sn}</td>
                <td>{$item.goods_name}</td>
                <td>{$item.shop_price}</td>
            </tr>
            {else}
                <tr >
                <td>{$item.goods_id}</td>
                <td>{$item.goods_sn}</td>
                <td>{$item.goods_name}</td>
                <td>{$item.shop_price}</td>
            </tr>
            {/if}
            {/foreach}
        </table>
        <pre>方法二</pre>
        <table height='400' cellspacing='0' cellpadding='0' border='1'>
            <tr>
                <th>编号</th>
                <th>商品序号</th>
                <th>商品名</th>
                <th>商品价格</th>
            </tr>
            {foreach $goodslist as $k=>$v}
            {if $v@first || $v@last}
            <tr style='background-color:#f69'>
                <td>{$v.goods_id}</td>
                <td>{$v.goods_sn}</td>
                <td>{$v.goods_name}</td>
                <td>{$v.shop_price}</td>
            </tr>
            {else}
                <tr >
                <td>{$v.goods_id}</td>
                <td>{$v.goods_sn}</td>
                <td>{$v.goods_name}</td>
                <td>{$v.shop_price}</td>
            </tr>
            {/if}
            {/foreach}
        </table>
 </body>
</html>

 /*
   smarty 循环之section\ while循环
 */

  /*
    基本的语法
        section 只用于索引数组
      {section loop=循环的数组 name=任意符合php变量的名字}
        name=index 代表每一次循环的键值 0  1 2 3
      {/section}
      显示时  $arr[index].键名
      {while 变量 条件}
      {$i++} or {$i--}
      {/while}
      smarty数学计算不支持{++$i} {--$i} 因此在while  for  if
      等都不能这样使用

  */

 <body>
   {literal}
    <pre>
        
    基本的语法 
        section 只用于索引数组
      {section loop=循环的数组 name=任意符合php变量的名字}
        name=index 代表每一次循环的键值 0  1 2 3
      {/section}
      显示时  $arr[index].键名
      {while 变量 条件}
      {$i++} or {$i--}
      {/while}
      smarty数学计算不支持{++$i} {--$i} 因此在while  for  if
      等都不能这样使用
    </pre>
    {/literal}
        <table height='400' cellspacing='0' cellpadding='0' border='1'>
            <tr>
                <th>编号</th>
                <th>商品序号</th>
                <th>商品名</th>
                <th>商品价格</th>
            </tr>
            {section loop=$goodslist name=i}
            {if  $smarty.section.i.first || $smarty.section.i.last}
            <tr style='background-color:#ccc'>
                <td>{$goodslist[i].goods_id}</td>
                <td>{$goodslist[i].goods_sn}</td>
                <td>{$goodslist[i].goods_name}</td>
                <td>{$goodslist[i].shop_price}</td>
            </tr>
            {else}
                <tr >
                <td>{$goodslist[i].goods_id}</td>
                <td>{$goodslist[i].goods_sn}</td>
                <td>{$goodslist[i].goods_name}</td>
                <td>{$goodslist[i].shop_price}</td>
            </tr>
            {/if}
            {/section}
        </table>
        <pre>while</pre>
        {while $num >=0}
            {$num--}<br/>
        {/while}
 </body>

 

posted @ 2017-04-04 17:36  yangboom  阅读(260)  评论(0编辑  收藏  举报
TOP