smarty模板变量

smarty变量
get:获得地址栏中的信息
post:获得表单提交内容
cookie session:保存变量的值
smarty的内置变量
foreach:smarty的保留变量,保存了所有的foreach循环,有name属性,iteration当前循环的次数
section:smarty保留变量,保存了所有的section循环,index保存数组的索引下标
even :偶数
odd:奇数
eq:等于
now :smart有时间戳
利用上面变量做一个,

从几到几,都是作的表单,是随机输入的数,通过post变量提交到num.php中,$_POST()获得后,把连续

的数变成一个数组传给num.tpl中,然后模板中进行判断奇偶输出。三个按钮,点击触发三个函数,通过

get获得地址栏中的信息,在num.php中获得,在赋给模板,(num.tpl中)用来判断是那个按钮,在模板

中foreach,section进行数组的遍历,在tpl中也用到变量foreanc,section的到数组的下标。
1、num.html
<script language="javascript" type="text/javascript">
function submit_even(){
 document.getElementById("num_scope").action="num.php?num=even";
 document.getElementById("num_scope").submit();
 }
function submit_odd(){
 document.getElementById("num_scope").action="num.php?num=odd";
 document.getElementById("num_scope").submit();
 }
function submit_else(){                         
 var num = document.getElementById("c_num").value;
document.getElementById("num_scope").action="num.php?num="+num;
 
 document.getElementById("num_scope").submit();
 }
</script>

<form action="" method="post" id="num_scope">
请输入数值范围<input type="text" name="num_start" size="4" />到<input type="text"

name="num_end" size="4" /><br />
</form>
<form action="" method="post" id="customer_num">
输出能被<input id="c_num" type="text"  size="4" />整除的数
</form>

<input value="输出偶数" type="button" οnclick="submit_even()" />&nbsp;&nbsp;
<input value="输出奇数" type="button" οnclick="submit_odd()" />
<input value="输出" type="button"  οnclick="submit_else()"/>

2、num.php
<?php
include("libs/Smarty.class.php");
$smarty=new Smarty();
$smarty->template_dir="demo/templates";
$smarty->compile_dir="demo/tempaltes_c";
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
$smarty->config_dir="demo/config";//配置文件存放路径

 


$start = $_POST["num_start"];
$end = $_POST["num_end"];
$num_cate = $_GET["num"];//smarty中get变量的应用,从地址栏中获得num的值
$array = array();
$count = $end-$start;


for($i=$start;$i<=$end;$i++){
 $array[]=$i;//数组下标默认从0开始
 }

$smarty->assign("array",$array);
$smarty->assign("num_cate",$num_cate);
$smarty->display("num.tpl");
?>
3、num.tpl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>


<{if $num_cate eq "even"}>

<{foreach from=$array item=value name="e"}>
<{if $value is even}>
数组中第<{$smarty.foreach.e.iteration}>个偶数是:<{$value}><br />
<{*$smarty.foreach:smarty的保留变量,保存了所有的foreach循环。iteration当前循环的次数*}>
<{/if}>
<{/foreach}>
<{elseif $num_cate eq "odd"}>
<{section name="odd" loop=$array}>
<{if $array[odd] is odd}>
数组中第<{$smarty.section.odd.index}>个偶数是:<{$array[odd]}><br /><{*odd代表是哪个session

,index数组中索引下标*}>
<{/if}>
<{/section}>
<{else}>
<{section name="el" loop=$array}>
<{if $array[el]%$num_cate==0}>
数组中第<{$smarty.section.el.index}>个能被<{$num_cate}>整除的数是:<{$array[el]}><br />
<{/if}>
<{/section}>
<{/if}>
</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值