ThinkPHP’s built-in template engine is an independently innovative XML compilation template engine. The following uses a case to share the usage of some commonly used template tags in ThinkPHP’s built-in template engine, including variable output, looping, judgment, comparison, etc. , these are relatively basic usages and cannot include all tags and features of ThinkPHP's built-in template engine.
(1) The following is the source code of the controller IndexAction class
<?php class IndexAction extends Action{ public function index() { $_SESSION['name'] = 'ThnkPHP Session'; $vo = array('id'=>1,'name'=>'ThinkPHP','email'=>'liu21st@gmail.com'); $this->assign('vo',$vo); $obj = (object)$vo; $this->assign('obj',$obj); $this->assign('array',array(5,260,13,7,40,50,2,1)); $this->assign('num1',6); $this->assign('num2',2); $this->assign('num',6); $this->display(); } } ?>
(2) The following demonstrates some basic usage of variables, constants and arrays assigned by the above controller to the template
Universal variable output
num1:{$num1}
Object output
id:{$obj:id}
name:{$obj:name}
Array output
id:{$vo['id']}
name:{$vo['name']}
Automatically determine array and object output
id:{$vo.id}
name:{$vo.name}
System constant output (system constants do not need to be assigned in the controller)
{$Think.now|date='Y-m-d H:i:s',###}
{$Think.server.PHP_SELF}
{$Think.session.name}
Use functions for variables (this can be built-in functions or custom functions)
{$vo.name|strtolower|ucwords}
Foreach output
{$key}:{$item}
The following example uses loop tags, Switch tags, and comparison tags
[{$i}]
{$val} is greater than 15
{$val} is less than 10
The output result is similar to:
[1] Odd numbered lines 5 less than 10 length 1
[2] Even lines 260 is greater than 5 260 is greater than 15 The length is 3
[3] Odd lines 13 is greater than 5 and length is 2
[4] Even lines 7 is greater than 5, 7 is less than 10, length is 1
[5] Odd lines 40 greater than 5 40 greater than 15 length 2
[6] Even lines 50 is greater than 5 50 is greater than 15 length is 2
[7] Odd lines 2 less than 10 and length 1
[8] Even lines 1 is less than 10 and length is 1
ThinkPHP built-in template engine comparison tag
The output result is similar to:
Greater than 3
Less than 3
Conditional judgment
{$num} is greater than 5
{$num} is greater than 3
Others{$num}
The output result is similar to:
6 is greater than 5