ThinkPHP模板IF标签用法详解
ThinkPHP的IF标签可以用来定义复杂的条件判断,例如:
<ifcondition="($nameeq1)OR($namegt100)">value1 <elseifcondition="$nameeq2"/>value2 <else/>value3 </if>
注意:在condition属性中可以支持eq等判断表达式,同上面的比较标签,但是不支持带有”>”、”<”等符号的用法,因为会混淆模板解析,所以下面的用法是错误的:
<ifcondition="$id<5">value1 <else/>value2 </if>
必须改成:
<ifcondition="$idlt5">value1 <else/>value2 </if>
除此之外,我们可以在condition属性里面使用php代码,例如:
<ifcondition="strtoupper($user['name'])neq'THINKPHP'">ThinkPHP <else/>otherFramework </if>
condition属性可以支持点语法和对象语法,例如自动判断user变量是数组还是对象:
<ifcondition="$user.nameneq'ThinkPHP'">ThinkPHP <else/>otherFramework </if>
或者知道user变量是对象
<ifcondition="$user:nameneq'ThinkPHP'">ThinkPHP <else/>otherFramework </if>
注意:由于if标签的condition属性里面基本上使用的是php语法,尽可能使用判断标签和Switch标签会更加简洁,原则上来说,能够用switch和比较标签解决的尽量不用if标签完成。因为switch和比较标签可以使用变量调节器和系统变量。如果某些特殊的要求下面,IF标签仍然无法满足要求的话,可以使用原生php代码或者PHP标签来直接书写代码。
更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《smarty模板入门基础教程》及《PHP模板技术总结》。
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。