Brief introduction of volist tag usage in Thinkphp

  • 2021-07-01 06:56:14
  • OfStack

Usually, volist tags are mostly used to output the results of querying data sets (select methods). Usually, the results returned by select methods of models are a 2-dimensional array, which can be directly output by volist tags.

In the controller, you first assign values to templates, as shown in the following example:


$User = M('User');
$list = $User->limit(10)->select();
$this->assign('list',$list);

In the template definition as follows, loop out the user's number and name:


<volist name="list" id="vo">
{$vo.id}:{$vo.name}<br/>
</volist>

The name attribute of the Volist tag represents the name of the variable assigned by the template, so it cannot be changed arbitrarily in the template file. id represents the current loop variable, which can be specified at will, but you need to ensure that it does not conflict with the name attribute, for example:


<volist name="list" id="data">
{$data.id}:{$data.name}<br/>
</volist>

Support the output of some data in the query results, such as the output of records 5 ~ 15:


<volist name="list" id="vo" offset="5" length='10'>
{$vo.name}
</volist>

Output even records:


<volist name="list" id="vo" mod="2" >
<eq name="mod" value="1">
{$vo.name}
</eq>
</volist>

The Mod property is also used to control the line wrapping of 1 record, for example:


<volist name="list" id="vo" mod="5" >
{$vo.name}
<eq name="mod" value="4"><br/></eq>
</volist>

Output prompt when it is empty:


<volist name="list" id="vo" empty=" There is no data yet " >
{$vo.id}|{$vo.name}
</volist>

The empty attribute does not support passing in the html syntax directly, but it can support variable output, such as:


$this->assign('empty','<span class="empty"> No data </span>');
$this->assign('list',$list);

Then use in the template:


<volist name="list" id="vo" empty="$empty" >
{$vo.id}|{$vo.name}
</volist>

Output loop variables:


<volist name="list" id="vo" key="k" >
{$k}.{$vo.name}
</volist>

If the key attribute is not specified, the loop variable i is used by default, for example:


<volist name="list" id="vo">
{$vo.id}:{$vo.name}<br/>
</volist>
0

If you want to output the index of an array, you can use the key variable directly. Unlike the loop variable, this key is determined by the data itself, not controlled by the loop, for example:


<volist name="list" id="vo">
{$vo.id}:{$vo.name}<br/>
</volist>
1

Template can be directly used to set the dataset, without assigning values to template variables in the controller to pass in dataset variables, such as:


<volist name="list" id="vo">
{$vo.id}:{$vo.name}<br/>
</volist>
2

Readers who are interested in thinkPHP can check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "smarty Template Introduction Basic Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: