陈力:传智播客古代 珍宝币 泡泡龙游戏开发第55讲:PHP smarty模板自定义函数

来源:互联网 发布:水利网络培训 编辑:程序博客网 时间:2024/05/17 02:17

陈力:传智播客古代 珍宝币 泡泡龙游戏开发第55讲:PHP smarty模板自定义函数

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

一、smarty 自定义函数
    怎样理解smarty这种通过标签的方式就能调用函数呢? PHP程序员能不能自己也编写这样的函数呢?
    请使用smarty自定义函数的机制,编写一个函数myfun1,通过调用该函数(可以输入必要的参数),完成循环输出5次"hello,贵阳网站建设", 要求内容,颜色,字体大小均可指定。函数的调用形式:<{myfun1 size="5" con="hello贵阳网站建设" times="5" }>
****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 自定义函数——块注册方式
     我们自己可不可以写函数,通过标签来调用?
     在tpl文件中,我们经常看到如下方式去通过标签调用函数。
<lable param="" ..>
</lable>
<{myfun2 属性1=”??” 属性2=”??” }>
hello,world
<{/myfun2}>
我们看看如何通过在tpl中以上面的方式来完成。
需求: 请使用smarty自定义函数的机制,编写一个函数myfun1,通过调用该函数(可以输入必要的参数),完成循环输出5次"hello, 贵阳网站建设", 要求内容,次数,字体大小均可指定。

//注册自定函数
$smarty->register_function(“myabc”,”abc”);
注册块函数
$smary->register_block(“myfun2”,” abc2”);

tpl文件
<br/>********取自定义函数(块方式)*********<br/>
<{mytest2 times="5" color="red" size="5"}>
hello 贵阳网站建设
<{/mytest2}>
<br/>
代码:
//自定义函数,块方式实现
function test2($args,$con){
 $str="";
 for($i=0;$i<$args['times'];$i++){
    $str.="<font color='".$args['color']."' size='".$args['size']."'>".$con."</font>";
 }
 return $str;
}
//注册块函数
$smarty->register_block("mytest2","test2");//参数表示调用名称和定义的名称.


三、smarty 自定义普通函数——插件形式
     自定义函数要通过注册的方式才能使用,如果通过插件的形式来增加自定义函数,则不需要注册即可使用。如何以插件的形式增加自定义函数?
     在smarty给出一个plugsin这个文件夹,在smarty/plugins/...文件夹下编写。我们可以编写一个插件函数,但是这个函数名和文件名有一个规范,必须遵守以下形式。
文件名的格式:function.自定义函数名.php
函数的名字:
function smarty_function_自定义函数名($params, &$smarty){
//写码.
}
例子: 根据times的数值重复显示'content'。
********************function.test.php******
<?php 
function smarty_function_test($args, $template){ 
$str="";
for($i=0;$i<$args['times'];$i++){
$str.="<br/><font size=7 color='blue'>".$args['content']."</font>";
}
return $str;
}
?>
****在tpl文件中使用*******
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
{test times="3" content="贵阳网站建设"}
</html>

******show.php*******调用tpl文件
<?php 
require_once 'smarty_inc.php';
$smarty->display("templates/index.tpl");
?>

四、smarty 自定义块函数——插件形式
在smarty给出一个plugsin这个文件夹,在smarty/plugins/...文件夹下编写。
例子:以块的方式添加一个插件,把上节的手动注册的块,同样要保证名字的规范。
文件名的格式: block.块名.php
函数的名字:
function smarty_block_块($params, $content, &$smarty){
//写码.
}

在smarty/plugins/...文件夹下编写
*****************block.test2.php*********
<?php 
function smarty_block_test2($args, $content, $template, &$repeat){ 
$str="";
for($i=0;$i<$args['times'];$i++){
$str.="<br/><font size='".$args['size']
."' color='".$args['color']."'>".$content."</font>";
}
return $str;
}
?>
****在tpl文件中使用*******
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
{test2 times="10" size="2" color="red"}
贵阳网站建设
{/test2}
</html>
******show.php*******调用某个tpl文件
<?php 
require_once 'smarty_inc.php';
$smarty->display("templates/index.tpl");
?>

【推荐阅读】陈力:传智播客古代 珍宝币 泡泡龙游戏开发第55讲:PHP smarty模板自定义函数

0 0
原创粉丝点击