smarty中的section和foreach

2024-04-16 19:08
文章标签 foreach section smarty

本文主要是介绍smarty中的section和foreach,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

section循环

section的运用了解:
1、循环一个简单的一维数组:
Example 7-30. Looping a simple array with {section}

<?php
$data = array(1000,1001,1002);
$smarty->assign('custid',$data);
?>

//customer和下面的foo可以随便命名,作用其实仅仅是一个index下标,用来引用数组中的元素
{section name=customer loop=$custid} 

id: {$custid[customer]}<br />
{/section}
<hr />

{section name=foo loop=$custid step=-1}
{$custid[foo]}<br />
{/section}

//输出
id: 1000<br />
id: 1001<br />
id: 1002<br />
<hr />
id: 1002<br />
id: 1001<br />
id: 1000<br />

2、不用assign数组直接在smarty中循环:
Example 7-31. {section} without an assigned array

//特别地设置了start,step属性用来控制循环
//$smarty.section.section的名字.index是一个特殊变量,用来显示当前循环的位置
{section name=foo start=10 loop=20 step=2}
{$smarty.section.foo.index}
{/section}
<hr />
{section name=bar loop=21 max=6 step=-2}
{$smarty.section.bar.index}
{/section}

//输出:
10 12 14 16 18
<hr />
20 18 16 14 12 10

3、section的name的值是随你定的
Example 7-32. Naming a {section}

{section name=anything loop=$myArray}
{$myArray[anything].foo}
{$name[anything]} //这种用法目前还没怎么用过,也不太清楚
{$address[anything].bar} //这种也是
{/section}

4、遍历一个关联数组,嵌套的数组

<?php
$data = array(
array('name' => 'John Smith', 'home' => '555-555-5555',
'cell' => '666-555-5555', 'email' => 'john@myexample.com'),
array('name' => 'Jack Jones', 'home' => '777-555-5555',
'cell' => '888-555-5555', 'email' => 'jack@myexample.com'),
array('name' => 'Jane Munson', 'home' => '000-555-5555',
'cell' => '123456', 'email' => 'jane@myexample.com')
);
$smarty->assign('contacts',$data);
?>

//section不用嵌套,因为只有一个数组,数组内部用$contacts[customer]得到
//每个数组,再用.键名来得到键值
{section name=customer loop=$contacts}
<p>
name: {$contacts[customer].name}<br />
home: {$contacts[customer].home}<br />
cell: {$contacts[customer].cell}<br />
e-mail: {$contacts[customer].email}
</p>
{/section}

The above example will output:

<p>
name: John Smith<br />
home: 555-555-5555<br />
cell: 666-555-5555<br />
e-mail: john@myexample.com
</p>
<p>
name: Jack Jones<br />
home phone: 777-555-5555<br />
cell phone: 888-555-5555<br />
e-mail: jack@myexample.com
</p>
<p>
name: Jane Munson<br />
home phone: 000-555-5555<br />
cell phone: 123456<br />
e-mail: jane@myexample.com
</p>
5、从数据库查询记录显示,实际上是显示二维数组,其实同上例一样
<?php
$sql = 'select id, name, home, cell, email from contacts '
."where name like '$foo%' ";
$smarty->assign('contacts', $db->getAll($sql));
?>

//结果:

<table>
<tr><th>&nbsp;</th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr>
{section name=co loop=$contacts} 
//第一维
<tr>
<td><a href="view.php?id={$contacts[co].id}">view<a></td> //第二维用.号来引用
<td>{$contacts[co].name}</td>
<td>{$contacts[co].home}</td>
<td>{$contacts[co].cell}</td>
<td>{$contacts[co].email}</td>
<tr>
{sectionelse}
<tr><td colspan="5">No items found</td></tr>
{/section}
</table>

6、嵌套的section
<?php

$id = array(1001,1002,1003);
$smarty->assign('custid',$id);

$fullnames = array('John Smith','Jack Jones','Jane Munson');
$smarty->assign('name',$fullnames);

$addr = array('253 N 45th', '417 Mulberry ln', '5605 apple st');
$smarty->assign('address',$addr);

$types = array(
array( 'home phone', 'cell phone', 'e-mail'),
array( 'home phone', 'web'),
array( 'cell phone')
);
$smarty->assign('contact_type', $types);

$info = array(
array('555-555-5555', '666-555-5555', 'john@myexample.com'),
array( '123-456-4', 'www.example.com'),
array( '0457878')
);
$smarty->assign('contact_info', $info);

?>

{section name=customer loop=$custid}
<hr>
id: {$custid[customer]}<br />
name: {$name[customer]}<br />
address: {$address[customer]}<br />
{section name=contact loop=$contact_type[customer]}
{$contact_type[customer][contact]}: {$contact_info[customer][contact]}<br />
{/section}
{/section}

The above example will output:

<hr>
id: 1000<br />
name: John Smith<br />
address: 253 N 45th<br />
home phone: 555-555-5555<br />
cell phone: 666-555-5555<br />
e-mail: john@myexample.com<br />
<hr>
id: 1001<br />
name: Jack Jones<br />
address: 417 Mulberry ln<br />
home phone: 123-456-4<br />
web: www.example.com<br />
<hr>
id: 1002<br />
name: Jane Munson<br />
address: 5605 apple st<br />
cell phone: 0457878<br />

smarty section 用法

{section loop = $varName[, start = $start, step = $step, max = $max, show = true]}

name: section的名称,不用加$
$loop: 要循环的变量,在程序中要使用assign对这个变量进行操作。
$start: 开始循环的下标,循环下标默认由0开始
$step: 每次循环时下标的增数
$max: 最大循环下标
$show: boolean类型,决定是否对这个块进行显示,默认为true

这里有个名词需要说明:
循环下标:实际它的英文名称为index,是索引的意思,这里我将它译成"下标",主要是为了好理解。它表示在显示这个循环块时当

前的循环索引,默认从0开始,受$start的影响,如果将$start设为5,它也将从5开始计数,在模板设计部分我们使用过它,这是当前

{section}的一个属性,调用方式为Smarty.section.sectionName.index,这里的sectionName指的是函数原型中的name属性。
{section}块具有的属性值,分别为:
1. index: 上边我们介绍的"循环下标",默认为0
2. index_prev: 当前下标的前一个值,默认为-1
3. index_next: 当前下标的下一个值,默认为1
4. first: 是否为第一下循环
5. last: 是否为最后一个循环
6. iteration: 循环次数
7. rownum: 当前的行号,iteration的另一个别名
8. loop: 最后一个循环号,可用在section块后统计section的循环次数
9. total: 循环次数,可用在section块后统计循环次数
10. show: 在函数的声明中有它,用于判断section是否显示

*foreach循环

1. foreach:用于循环简单数组,它是一个选择性的section循环,它的定义格式为:

{foreach from=$array item=array_id}
{foreachelse}
{/foreach}
其中,from 指出要循环的数组变量,item为要循环的变量名称,循环次数由from所指定的数组变量的个数所决定。{foreachelse}用来当程序中传递过来的数组为空时的处理,下面是一个简单的例子:
===========================================
example6.tpl
===========================================
<html>
<head><title>这是一个foreach使用的例子</title></head>
<body>
这里将输出一个数组:<br>
<{foreach from=$newsArray item=newsID}>
新闻编号:<{$newsID.newsID}><br>
新闻内容:<{$newsID.newsTitle}><br><hr>
<{foreachelse}>
对不起,数据库中没有新闻输出!
<{/foreach}>
</body>
</html>

这篇关于smarty中的section和foreach的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/909647

相关文章

ecshop源码分析:smarty模板类

花了差不多3天半的时间,基本上把ecshop1400多行的模板类源码阅读完毕。从构造函数一行一行的阅读下去,遇到方法的调用便进去,遇到返回值又回到调用它的地方,这样来回不知道多少遍,每次阅读都让我心奋不已。之前一直都在使用smarty,却不知道它的工作原理,现在终于大概有个详细并全面的了解了,怎能不兴奋。现在,给大家分享我在看源码总结出的smarty的工作流程以及smarty标签的使用

mybatis中使用foreach构造多like查询及批量插入

使用foreach批量查询: <!--wc根据商品分类名字,查询检测能力模糊得到数据 --><select id="likeGoodsType" resultMap="goodstypeMap">SELECT <include refid="proAll"/> FROM goods_type WHERE 1>2 OR<foreach collection="array" item="ite

PHP foreach数组循环的一些问题

点击打开链接打开官方手册对foreach的介绍 <?php/*PHP foreach() 语法结构用于遍历操作或输出数组,foreach() 仅能用于遍历数组或对象,当试图将其用于其它数据类型或者一个未初始化的变量时会产生错误。语法:*/foreach (array as $value)statement// 或者:foreach (array as $key => $

torch.optim 之 Algorithms (Implementation: for-loop, foreach, fused)

torch.optim的官方文档 官方文档中文版 一、Implementation torch.optim的官方文档在介绍一些optimizer Algorithms时提及它们的implementation共有如下三个类别:for-loop, foreach (multi-tensor), and fused。 Chat-GPT对这三个implementation的解释是: For-loo

configparser.DuplicateSectionError: While reading from '/home/qinghua/.theanorc' [line 18]: section

python代码: import theano 出现错误: configparser.DuplicateSectionError: While reading from '/home/qinghua/.theanorc' [line 18]: section 'nvcc' already exists 解决方法是, vim ~/.theeanorc 删除行: [nvcc]

Error Loading extension section usr_cert

在用easy_rsa生成ovpn配置时,出现如下错误: [ root: /usr/share/easy-rsa] #/usr/share/easy-rsa/build-key --batch zzzz.29761Using Common Name: zzzz.29761Generating a 2048 bit RSA private key...............+++.....

PHP关于foreach按引用循环,最后一个值和前一个相同的问题

执行代码: <?php $a=[4,5,6,7]; foreach ($a as $k=>&$v){ var_dump($a); echo "<br/>"; } echo "<br/>"; foreach ($a as $k=>$v){     print_r($a);     echo "<br/>"; } echo "<br/>"; print_r($a);   执行过程和结果: a

tableView section随cell移动 ,不在顶到屏幕顶部

加上这段代码即可  func scrollViewDidScroll(scrollView:UIScrollView) {         let sectionHeaderHeight:CGFloat = 55         if scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.con

【解决】如何在JavaScript中终止forEach循环

已解决在 JavaScript 中跳出forEach循环。 一、问题产生的现象     在项目开发中遇到这样一个问题,在JavaScript 代码中使用到了forEach的循环方式,在循环中当不满足某个条件时需要终止循环并进行提示,然后forEach循环与for和while循环不一样,直接使用break或者return都无法终止循环。 比如: const array = [ -3, -

Codeigniter整合smarty

smarty的模板机制很强大,一般情况下CI无需整合其他模板标签,因为PHP本身就是一种标签,简单易用。codeigniter整合smarty教程(我用的都是最新版本)如下: 第一步:下载codeigniter最新版本: http://codeigniter.org.cn/downloads 第二步:下载smarty最新版本: http://www.smarty.net/downl