Home  >  Article  >  Backend Development  >  How to use TP5.1 template loop tag (code)

How to use TP5.1 template loop tag (code)

不言
不言Original
2018-08-20 16:52:378155browse

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 = [
            &#39;user1&#39;=>[
                &#39;name&#39; =>&#39;imooc&#39;,
                &#39;email&#39;=>&#39;imooc@qq.com&#39;
            ],
            &#39;user2&#39;=>[
                &#39;name&#39; =>&#39;104&#39;,
                &#39;email&#39;=>&#39;104@qq.com&#39;
            ],
            &#39;user3&#39;=>[
                &#39;name&#39;=>&#39;cjk&#39;,
                &#39;email&#39;=>&#39;cjk@qq.com&#39;
            ]

        ];

        $this->assign(&#39;list&#39;,$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=&#39;list&#39; id=&#39;vo&#39; offset=&#39;0&#39; length=&#39;3&#39;}
		<p>{$key} : {$vo.name} : {$vo.email}</p>
	{/volist}


第二种foreach
name=assign中的变量名
item=数组中的key
key=数组中的下标
	{foreach name=&#39;list&#39; item=&#39;vo&#39; key=&#39;kkk&#39;}
		<p>{$kkk} : {$vo.name}</p>
	{/foreach}



第三种for
start=开始循环的位置
end=结束循环的位置
step=步长
name=for循环中的$i
	{for start=&#39;1&#39; end=&#39;10&#39; step=&#39;2&#39; name=&#39;k&#39;}
		<p>{$k}</p>
	{/for}

	
	<!-- 默认name -->
	{for start=&#39;1&#39; end=&#39;10&#39;}
		<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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn