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

/*
   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>

模板引擎(smarty)知识点总结五的更多相关文章

  1. PHP的模板引擎smarty原理是什么(整理)

    PHP的模板引擎smarty原理是什么(整理) 一.总结 一句话总结:其实所有的模板引擎的工作原理是差不多的,无非就是在php程序里面用正则匹配将模板里面的标签替换为php代码从而将两者混合为一个ph ...

  2. PHP模板引擎Smarty内建函数section,sectionelse用法详解

    本文实例讲述了PHP模板引擎Smarty内建函数section,sectionelse用法.分享给大家供大家参考,具体如下: section 是 Smarty 模板中除了 foreach 以外的另一种 ...

  3. Php模板引擎Smarty安装和配置

    Smarty 是PHP的一个模板引擎,是由Monte Ohrt 和 Andrei Zmievski 使用PHP语言开发的,发展至今已成为一个非常流行的模板引擎,Smarty 提供了一种易于管理和使用的 ...

  4. php模板引擎smarty

    一. smarty的特点 速度:相对于其他模板引擎,速度较快 编译型:在下次访问模板时直接访问编译文件,不再进行模板重新编译 缓存技术:可以将用户最终看到的HTML文件缓存成一个静态HTML 插件技术 ...

  5. php模板原理PHP模板引擎smarty模板原理浅谈

    mvc是开发中的一个伟大的思想,使得开发代码有了更加清晰的层次,让代码分为了三层各施其职.无论是对代码的编写以及后期的阅读和维护,都提供了很大的便利. 我们在php开发中,视图层view是不允许有ph ...

  6. PHP的模板引擎smarty原理浅谈

    mvc是开发中的一个伟大的思想,使得开发代码有了更加清晰的层次,让代码分为了三层各施其职.无论是对代码的编写以及后期的阅读和维护,都提供了很大的便利. 我们在php开发中,视图层view是不允许有ph ...

  7. Smarty模板引擎技术

    Smarty模板引擎技术 什么是模板引擎? 什么是Smarty模板引擎? 为何选择Smarty模板引擎? 如何使用Smarty模板引擎? 一.历史背景 场景一:回顾之前编写PHP项目的方式 //链接数 ...

  8. 推荐13款javascript模板引擎

    javaScript 在生成各种页面内容时如果能结合一些模板技术,可以让逻辑和数据之间更加清晰,本文介绍 X 款 JavaScript 的模板引擎.(排名不分先后顺序) 1. Mustache 基于j ...

  9. js模板引擎介绍搜集

    js模板引擎越来越多的得到应用,如今已经出现了几十种js模板引擎,国内各大互联网公司也都开发了自己的js模板引擎(淘宝的kissy template,腾讯的artTemplate,百度的baiduTe ...

  10. php的模板引擎

    设计一个交互式的网站,我们需要关注两个主要的问题:分别是图形用户界面和业务逻辑.例如,一个标准的web开发小组由两三个美工和三个程序员组成,则设计流程是:美工设计者制作了项目的网站的界面模板,然后把它 ...

随机推荐

  1. 逆向知识第十四讲,(C语言完结)结构体在汇编中的表现形式

    逆向知识第十四讲,(C语言完结)结构体在汇编中的表现形式 一丶了解什么是结构体,以及计算结构体成员的对其值以及总大小(类也是这样算) 结构体的特性 1.结构体(struct)是由一系列具有相同类型或不 ...

  2. C++ 指针和引用 吐血整理 Pointer&Reference

    说道C++的指针,很多人都很头疼,也很confuse.经常把它和变量名,引用(reference)等混淆,其实这最主要的原因是很多程序员对于基本知识的掌握有问题,从而导致的很多基本概念的混淆.本文就是 ...

  3. linux 常用命令详解

    常见Linux目录名称:/ 虚拟目录的根目录.通常不会在这里存储文件/bin 二进制目录,存放许多用户级的GNU工具/boot 启动目录,存放启动文件/dev 设备目录,Linux在这里创建设备节点/ ...

  4. dijkstra最小花费

    //Gang #include<iostream> #include<cstring> #include<algorithm> #include<cstdio ...

  5. 读懂源码:一步一步实现一个 Vue

    源码阅读:究竟怎样才算是读懂了? 市面上有很多源码分析的文章,就我看到的而言,基本的套路就是梳理流程,讲一讲每个模块的功能,整篇文章有一大半都是直接挂源码.我不禁怀疑,作者真的看懂了吗?为什么我看完后 ...

  6. 工程启动加载.properties/.xml配置文件

    工程目录: demo.properties today=2017-06-10 PropertiesMap.java package com.dzpykj.common.utils; import ja ...

  7. 50个php程序性能优化集锦

    1. 用单引号代替双引号来包含字符串,这样做会更快一些.因为 PHP 会在双引号包围的 字符串中搜寻变量,单引号则不会,注意:只有 echo 能这么做,它是一种可以把多个字符 串当作参数的" ...

  8. Angular4.0引入laydate.js日期插件方法

    Angular是不支持直接引入js文件的,下面介绍项目如果引入laydate.js的方法 一.将下载的laydate中的js和theme文件放到一个统一的文件下面,我把它放到asset下 二.在ang ...

  9. 为并发而生的 ConcurrentHashMap(Java 8)

    HashMap 是我们日常最常见的一种容器,它以键值对的形式完成对数据的存储,但众所周知,它在高并发的情境下是不安全的.尤其是在 jdk 1.8 之前,rehash 的过程中采用头插法转移结点,高并发 ...

  10. 如何配置 Health Check?- 每天5分钟玩转 Docker 容器技术(107)

    容器状态是 UP 的,应用就是健康的吗? 还真不一定!Docker 只能从容器启动进程的返回代码判断其状态,而对于容器内部应用的运行情况基本没有了解. 执行 docker run 命令时,通常会根据 ...