ThinkPHP5开发:模板中系统函数和自定义函数的使用

2023-01-1808:47:43后端程序开发Comments1,204 views字数 1268阅读模式

1.在模板中使用php函数文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

在thinkphp的html中,我们经常会遇到一些变量难以直接从php控制端直接处理,这些变量只有在模板中循环输出的时候处理比较合适,这个时候,我们就要在模板中使用函数文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

1.1对输出模板使用php函数文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

1
{$data.name|md5} //把模板中的name变量进行md5加密

把这句话翻译成php语言:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

1
<?php echo (md5($data['name'])); ?>

1.2函数中多个参数需要调用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

1.2.1将前面输出的变量当后面函数的第二个参数使用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

1
{$create_time|date="Y-m-d H:i:s ",###}

解释:date函数传入两个参数,每个参数用逗号分割,这里第一个参数是y-m-d,第二个参数是前面要输出的create_time变量,因为该变量是第二个参数,因此需要用###标识变量位置。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

翻译成php语言:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

1
<?php echo (date("Y-m-d H:i:s ",$create_time)); ?>

1.2.2将前面输出的变量当后面函数的第一个参数使用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

1
{$data.name|substr=0,3}

或者文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

1
{$data.name|substr=###,0,3}

翻译成php语言文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

1
<?php echo (substr($data['name'],0,3)); ?>

1.3对一个变量进行多个函数的处理文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

1
{$name|md5|strtoupper|substr=0,3}

每个函数之间用丨符号隔开,且函数执行顺序是从左往右依次调用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

或者:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

1
{:substr(strtoupper(md5($name)),0,3)}

编译成php语言:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

1
<?php echo (substr(strtoupper(md5($name)),0,3)); ?>

2.变量在模板中输出使用自定义函数文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

在项目中,除了一些php函数,我们也可以根据自己项目的实际需求,在项目应用目录/common/function.php中,写入自己自定义的函数文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

重点说明:{ 和 $ 符号之间不能有空格,后面参数的空格就没有问题;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

###表示模板变量本身的参数位置 ;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

支持多个函数,函数之间支持空格 ;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

支持函数屏蔽功能,在配置文件中可以配置禁止使用的函数列表 ;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

支持变量缓存功能,重复变量字串不多次解析。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

2.1自定义函数的使用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

简单的自定义函数和使用php函数基本一致。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

2.2自定义函数的高级使用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

1
{$varname|function1|function2=arg1,arg2,### }

翻译成php代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

1
<?php echo (function2(function1(arg1,arg2,$varname)); ?>

2.3案例:我在common.php中写入一个方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

1
2
3
4
5
function Cate($cid){ 
$Cate=D('Cate'); 
$Cate=$Cate->where('id='.$cid)->find(); 
return $Cate['title']; 
}

在模板中调用这个自定义函数:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html

1
{$vo.cid|cate=###}
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30503.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/30503.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定