Home > Article > Backend Development > How to use TP5.1 template loop tag (code)
The content of this article is about how to use TP5.1 template loop tags (code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
<?php namespace app\test\controller; use think\Controller; class Index extends Controller { public function index() { $list = [ 'user1'=>[ 'name' =>'imooc', 'email'=>'imooc@qq.com' ], 'user2'=>[ 'name' =>'104', 'email'=>'104@qq.com' ], 'user3'=>[ 'name'=>'cjk', 'email'=>'cjk@qq.com' ] ]; $this->assign('list',$list); return $this->fetch(); } }
THINPHP5.1 provides three ways for us to use loops in the view
{volist}{/volist}
{foreach}{/foreach}
{for}{/for}
第一种volist name=assign中的变量名 id=数组中的key offset=开始循环的位置 length=步长 {volist name='list' id='vo' offset='0' length='3'} <p>{$key} : {$vo.name} : {$vo.email}</p> {/volist} 第二种foreach name=assign中的变量名 item=数组中的key key=数组中的下标 {foreach name='list' item='vo' key='kkk'} <p>{$kkk} : {$vo.name}</p> {/foreach} 第三种for start=开始循环的位置 end=结束循环的位置 step=步长 name=for循环中的$i {for start='1' end='10' step='2' name='k'} <p>{$k}</p> {/for} <!-- 默认name --> {for start='1' end='10'} <p>{$i}</p> {/for}
You can use {php}{/php} to insert php code
Related recommendations:
TP5. 1. Use function method in view (code)
php to generate mixed verification code and image verification code and test (code)
The above is the detailed content of How to use TP5.1 template loop tag (code). For more information, please follow other related articles on the PHP Chinese website!