Usually the volist tag is mostly used to output the results of querying the data set (select method). Usually the result returned by the select method of the model is a two-dimensional array, for which the volist tag can be used directly for output.
First assign a value to the template in the controller, as shown in the following example:
$User = M('User'); $list = $User->limit(10)->select(); $this->assign('list',$list);
The template is defined as follows, and the user’s number and name are output in a loop:
<volist name="list" id="vo"> {$vo.id}:{$vo.name}<br/> </volist>
The name attribute of the Volist tag represents the variable name assigned by the template, so it cannot be changed at will in the template file. id represents the current loop variable, which can be specified at will, but you need to make sure not to conflict with the name attribute, for example:
<volist name="list" id="data"> {$data.id}:{$data.name}<br/> </volist>
Supports outputting part of the data in the query results, for example, outputting the 5th to 15th records:
<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>
Mod attribute is also used to control the line wrapping of certain records, for example:
<volist name="list" id="vo" mod="5" > {$vo.name} <eq name="mod" value="4"><br/></eq> </volist>
is empty, output prompt:
<volist name="list" id="vo" empty="暂时没有数据" > {$vo.id}|{$vo.name} </volist>
empty attribute does not support direct passing of HTML syntax, but can support variable output, for example:
$this->assign('empty','<span class="empty">没有数据</span>'); $this->assign('list',$list);
Then use in the template:
<volist name="list" id="vo" empty="$empty" > {$vo.id}|{$vo.name} </volist>
Output loop variable:
<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" > {$i}.{$vo.name} </volist>
If you want to output the index of the array, you can use the key variable directly. Unlike the loop variable, the key is determined by the data itself, not controlled by the loop, for example:
<volist name="list" id="vo" > {$key}.{$vo.name} </volist>
You can directly use functions to set data sets in templates, without assigning values to template variables in the controller and passing in data set variables, such as:
<volist name=":fun('arg')" id="vo"> {$vo.name} </volist>