陈力:传智播客古代 珍宝币 泡泡龙游戏开发第54讲:PHP smarty模板内建函数

陈力:传智播客古代 珍宝币 泡泡龙游戏开发第54讲:PHP smarty模板内建函数

描述: smarty模板技术为PHP程序设计中处理和界面的分离提供了很大的便利,有必要了解内建函数等方面的内容,方便贵阳网站建设人员加深对smarty模板处理机制的了解。陈力:传智播客古代 珍宝币 泡泡龙游戏开发第54讲:PHP smarty模板内建函数

一、什么是内建函数
     内建函数是无需定义可以直接使用的函数。smarty模板引擎提供了一系列的内建函数(bulid-in functions) 。smarty模板文件是以标签的方式来调用函数。

软件设计,贵阳网站建设,PHPsmarty内建函数


     这里我们先给大家介绍对数组进行操作的 foreach ,foreachelse 函数的使用,其余都会陆续介绍。
(1)搭建环境
     在网站\htdocs\smarty目录中,创建文件testcontroller2.php,在templates目录中创建test2.tpl。smarty放在./libs/。
(2)取普通索引数组
     在testcontroller2.php中加代码:
  require_once "./libs/smarty.class.php";
  $smarty=new smarty();
  $smarty->left_delimiter="<{";//一般都要写在Display方法之前。
  $smarty->right_delimiter="}>";
  $arr1=array('上海','天津','北京');
  $smarty->assign("arr1",$arr1);
$smarty->display("test2.tpl");
在test2.tpl文件中加代码:
<h1>内建函数</h1>
<br/>********取内建普通索引数组列表*********<br/>
<{foreach from=$arr1 item=temp}>
<{$temp}>
<{/foreach}>
<br/>
输出:
********取内建索引数组列表*********
上海天津北京
也可以带key来取值,<{foreach from=$arr1 item=temp key=kk}>
<{$kk}>=<{$temp}>
输出:
0=上海1=天津2=北京
(3)取普通关联一维数组
可以直接取出。
在testcontroller2.php中加代码:
  $arr2=array('city1'=>'上海','city2'=>'天津','city3'=>'北京');
  $smarty->assign("arr2",$arr2);
$smarty->display("test2.tpl");
在test2.tpl文件中加代码:
<br/>********取普通关联一维数组列表*********<br/>
<{foreach from=$arr2 item=temp}>
<{$temp}>
<{/foreach}>
<br/>
(4)取普通关联一维数组(带键值)
在testcontroller2.php中加代码:
$arr2=array('city1'=>'上海','city2'=>'天津','city3'=>'北京');
  $smarty->assign("arr2",$arr2);
$smarty->display("test2.tpl");
在test2.tpl文件中加代码:
<br/>********取普通关联一维数组列表(带键值)*********<br/>
<{foreach from=$arr2 item=temp key=k}>
<{$k}>=<{$temp}>
<{/foreach}>
<br/>
输出:
********取普通关联一维数组列表(带键值)*********
city1=上海 city2=天津 city3=北京
(5)取二维索引数组
在testcontroller2.php中加代码:
//索引的二组数据
  $arr3=array(array('上海','天津','北京'),array('小债','老妖','财神'));
  $smarty->assign("arr3",$arr3);
$smarty->display("test2.tpl");
在test2.tpl文件中加代码:
<br/>********取二维索引数组列表*********<br/>
<{foreach from=$arr3 item=temp key=k}>
<{$temp[0]}>||<{$temp[1]}>||<{$temp[2]}>||<br/>
<{/foreach}>
<br/>
输出:
********取二维索引数组列表*********
上海||天津||北京|| 
小债||老妖||财神||
当内容数组少一个或者多一个的时候,就取出出错。即某个数组没有值时产生错误,取不出来,也不报错。这种方式不太灵活。
(6)取二维索引数组(带子循环)
可以对temp进行再次循环。
在testcontroller2.php中加代码:
    $arr3=array(array('上海','天津','北京'),array('小债','老妖','财神','黄世仁'));
  $smarty->assign("arr3",$arr3);
在test2.tpl文件中加代码:
<br/>********取二维索引数组列表(带子循环)*********<br/>
<{foreach from=$arr3 item=temp key=k}>
<{foreach from=$temp item=val}>
<{$val}>
<{/foreach}>
<{/foreach}>
<br/>
 输出:
********取二维索引数组列表(带子循环)*********
上海天津北京小债老妖财神黄世仁
(7)取二维关联数组
在testcontroller2.php中加代码:
//关联的二组数据
$arr4=array(array('id'=>'a001','email'=>'xiaoin@sqq.com','age'=>23),array('id'=>'a002','email'=>'12333@sqq.com','age'=>66));
  $smarty->assign("arr4",$arr4);
$smarty->display("test2.tpl");
在test2.tpl文件中加代码:
<br/>********取二维关联数组列表*********<br/>
<{foreach from=$arr4 item=temp1 key=k}>
<{foreach from=$temp1 item=temp2 key=kk}>
<{$kk}>=<{$temp2}>
<{/foreach}>
<br/>
<{/foreach}>
<br/>
输出:
********取二维关联数组列表*********
id=a001 email=xiaoin@sqq.com age=23 
id=a002 email=12333@sqq.com age=66

(8)foreach  if—elseif—else
显示多个人物信息,可以从数据库取出,这里简单演示即可。
$emplist=array(array('id'=>'100','name'=>'小明',"age"=>40),array('id'=>'110','name'=>'大明',"age"=>80));
要求使用smarty模板,显示所有学员信息,但是不显示年龄。
在调用函数的时候,如果在模板tpl的双引号中使用到php文件的各种数据,参看双引号里值的嵌入章节(第53讲:PHP smarty模板配置及变量操作, http://www.by-sj.com/WebRes/Pro789.html)。
在testcontroller2.php中加代码:
$arr4=array(array('id'=>'a001','email'=>'xiaoin@sqq.com','age'=>12),array('id'=>'a002','email'=>'12333@sqq.com','age'=>66));
  $smarty->assign("arr4",$arr4);
在test2.tpl文件中加代码:
<br/>********foreach  if—elseif—else*********<br/>
<h1>取出二维关联数组{age<5 提示你是小孩 age>=5 age<=18 年轻人   age>18 成年人}</h1>
<{foreach from=$arr4 item=item1}>
<{*二次循环*}>
<{if $item1.age < 5}>
<{$item1.id}> 你是小孩
<{elseif $item1.age >=5 and $item1.age<=18 }>
<{$item1.id}>年轻人
<{elseif $item1.age>18}>
<{$item1.id}>成年人
<{/if}>
<br/>
<{/foreach}>
输出:
********foreach if—elseif—else*********
取出二维关联数组{age<5 提示你是小孩 age>=5 age<=18 年轻人 age>18 成年人}
a001年轻人 
a002成年人

(9)内建函数的综合性案例
**************循环取出emplist不显示age <br/>
<{foreach from=$emplist item=emp}>
<{foreach from=$emp item=val key=k}>
<{if $k != "age"}>
<{$k}>=<{$val}>
<{/if}>
<{/foreach}>
<br/>
<{/foreach}>
* 显示 多个人物信息.
***show.php***页面
<?php 
 include_once './libs/smarty.php';
 $test2[]=array("name"=>"张平","date"=>"1999-11-11");
 $test2[]=array("name"=>"张平2","date"=>"2000-11-11");
 $smarty->assign("title2",$test2);
 $test3=array("北京","天津","上海");
 $smarty->assign("test3",$test3);
 //如果给出的数组有键值对,我们怎么处理,比如我们表示一个人的信息
 $test4=array("name"=>"红本","电话"=>"120","email"=>"zhangsan@sohu.com");
 $smarty->assign("test4",$test4); 
 $smarty->display("index.tpl");
 //var_dump($test2);
?>
****对应的index.tpl文件*****
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
<hr/>
********循环取出变量**************<br/>
{section name=abc loop=$title2}
{$title2[abc].name}-{$title2[abc].date}<br/>
{/section}
<hr/>
{* 这个是注释哈哈 *}
********从数组单个单个的取出值************<br/>
{$test3[0]} | {$test3[1]} | {$test3[2]}<br/>
*********使用foreach取出数组的值*********<br/>
{foreach from=$test3 item=city_name}
城市名: {$city_name}<br>
{/foreach}
*********使用foreach取出数组的key-val对<br/>
{foreach from=$test4 item=item key=key}
{if $key neq "email"}
个人信息 : {$key} : {$item}<br/>
{else}
这个是email 信息,不能给你看!
{/if}
{/foreach}<br/>
***********变量操作符**************<br/>
{"now"|date_format:"%Y/%m/%d"}
{$smarty.now|date_format:"%Y/%m/%d %H:%M:%S"}
</body>
</html>
***********怎么理解smarty这种函数调用机制.*********
****show.php****
<?php 
require_once 'smarty_inc.php';
//这是注册一个函数.
$smarty->register_function("myfun1","test");
//自定义函数
function test($args){
$str="";
for($i=0;$i<$args['times'];$i++){
$str.="<br/><font size=7 color='blue'>".$args['content']."</font>";
}
return $str;
}
$smarty->display("templates/index.tpl");
?>
***index.tpl****
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
{myfun1 times="5" content="hello,张三"}
</html>
***smarty还有一种块方式调用标签的方式*******
**show.php****
<?php 
require_once 'smarty_inc.php';
$smarty->register_function("myfun1","test");
//自定义函数
function test($args){
$str="";
for($i=0;$i<$args['times'];$i++){
$str.="<br/><font size=7 color='blue'>".$args['content']."</font>";
}
return $str;
}
$tt="水浒传好看";
$smarty->assign("con1",$tt);
//smarty 块的方式调用函数.
$smarty->register_block("myfun2","test2");
//test2($args,$content,&$t,&$j) 块函数后面两个参数不用传递
function test2($args,$content){
$str="";
for($i=0;$i<$args['times'];$i++){
$str.="<br/><font size='".$args['size']
."' color='".$args['color']."'>".$content."</font>";
}
return $str;
}
$smarty->display("templates/index.tpl");
?>
****对应的index.tpl模板文件***
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
{myfun1 times="5" content="hello,张三"}
{myfun2 times="2" color="green" size="7"}
北京{$con1}
{/myfun2}
</html>

【推荐阅读】



陈力:传智播客古代 珍宝币 泡泡龙游戏开发第54讲:PHP smarty模板内建函数

深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值