这篇文章主要介绍了ThinkPHP模板之变量输出、自定义函数与判断语句用法,是关于ThinkPHP模板操作中非常实用的技巧,需要的朋友可以参考下
本文实例讲述了ThinkPHP模板之变量输出、自定义函数与判断语句用法。主要包括变量输出、自定义函数与判断语句三种用法。分享给大家供大家参考。具体分析如下:
模板操作变量输出:
快捷输出变量
复制代码 代码如下:
{:function(…)} //执行方法并输出返回值
{~function} //执行方法不输出
{@var} //输出Session变量
{#var} //输出Cookie变量
{&var} //输出配置参数
{%var} //输出语言变量
{.var} //输出GET变量
{^var} //输出POST变量
{*var} //输出常量
模版输出
复制代码 代码如下:
//1 直接调用index操作模板
$this->display();
// 对应Myapp/Tpl/default/Index/index.html
//2 调用Index模块的test1操作模板
$this->display('test1');
//对应Myapp/Tpl/default/Index/test1.html
//2 调用Message模块的test2操作模板()
$this->display('Message:test2');
//对应Myapp/Tpl/default/Message/test2.html
//3 调用XP主题的Message模块的test2操作模板
$this->display('Xp@Message:test2');
//对应Myapp/Tpl/Xp/Message/test2.html
//4 直接指定模板文件的全名
$this->display('../Message/test3.html');
//对应Myapp/Tpl/default/Message/test3.html
使用判断语句
我们可以使用if标签来定义复杂的条件判断,例如:
复制代码 代码如下:
在condition属性中可以支持eq等判断表达式 ,同上面的比较标签,但是不支持带有”>”、”
必须改成:
复制代码 代码如下:
除此之外,我们可以在condition属性里面使用php代码,例如:
复制代码 代码如下:
condition属性可以支持点语法和对象语法,例如:
自动判断user变量是数组还是对象
复制代码 代码如下:
或者知道user变量是对象
复制代码 代码如下:
由于if标签的condition属性里面基本上使用的是php语法,尽可能使用判断标签和Switch标签会更加简洁,原则上来说,能够用switch和比较标签解决的尽量不用if标签完成。因为switch和比较标签可以使用变量调节器和系统变量。如果某些特殊的要求下面,IF标签仍然无法满足要求的话,可以使用原生php代码或者PHP标签来直接书写代码。
eq 等于(==)
neq 不等于(!=)
gt 大于(>)
egt 大于等于(>=)
lt 小于(
elt 小于等于(
heq 恒等于(===)
nheq 不恒等于(!==)
condition 条件
注意
condition 属性值中,变量是需要 $ 符号的,这与其他标签不同。
使用自定义函数的方法
模板变量的函数调用格式:{$varname|function1|function2=arg1,arg2,### }
使用例子:
复制代码 代码如下:
{$webTitle|md5|strtoupper|substr=0,,3}
{$number|number_format=2}
{$varname|function1|function2=arg1,arg2,### }
实例如下:
复制代码 代码如下:
function Cate($cid){
$Cate=D('Cate');
$Cate=$Cate->where('id='.$cid)->find();
return $Cate['title'];
}
我想在模板中调用这个函数则在模板中可以这样写
复制代码 代码如下:
{$vo.cid|cate=###}
注意:自定义函数要放在项目应用目录/common/common.php中。 这里是关键。
说明:
{ 和 $ 符号之间不能有空格,后面参数的空格就没有问题;
###表示模板变量本身的参数位置 ;
支持多个函数,函数之间支持空格 ;
支持函数屏蔽功能,在配置文件中可以配置禁止使用的函数列表 ;
支持变量缓存功能,重复变量字串不多次解析。
希望本文所述对大家的ThinkPHP框架程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.