ThinkPhp5 中Volist标签的用法

Volist标签一般是和内置方法assign()搭配使用,将值从后台传到前台,是当下比较流行的一种传值方法

本文实例讲述了ThinkPHP模板循环输出Volist标签用法。分享给大家供大家参考,具体如下

在controller中,assign()方法是在Controller给View传值的函数:

    public function index()
    {
        //查找结果集
        $data['news'] = Db::query('select * from news order by news_time desc limit 7 ');
        //把结果集的索引换成自定义索引(id)
        $data['news'] = $this->update_index1($data['news'],'id');
       //传值到模板
        $this->assign('data',$data);
        //模板调用
        return $this->fetch();
    }

这时,值被传递到了模板,可以用Volost()标签调用:

                {volist name='data.news' id='vo'}
                <div class="content">
                    <div class="tu"><img src="{$vo.news_pic}"></div>
                    <div class="title"><span>{$vo.news_title}</span></div>
                    <div class="neirong"><p>{:strlen($data['news'][$vo.id]['news_body'])<60?$data['news'][$vo.id]['news_body']:substr($data['news'][$vo.id]['news_body'],0,200)}</p></div>
                    <div class="contentinfo"><span>{$vo.news_time}</span><span>xx条评论</span><span>xxx次阅读</span><span>{$vo.news_autuor}</span>
                        <a  href=? class="quanwen">>>阅读全文</a></div>
                </div>
                {/volist}
 {volist name='data.news' id='vo'}详解:
    name(必须):要输出的数据模板变量,name='data.news'需要与操作中的模板赋值指定对应$data['news']
    id(必须):循环变量,可以任意指定
  
{}标签中默认只能输出vo数组中的元素的值,{vo.xx},在php中代表$data['news']['xx'],
{:}里面可以包含方法,方便用三元运算符判断值,例如{:isset($data['news'][$vo.id]['title'])?$data['news'][$vo.id]['title']:''}



另外:volist标签中还有这些属性,了解即可

    offset(可选):要输出数据的offset

    length(可选):输出数据的长度

    key(可选):循环的key变量,默认值为i

    mod(可选):对key值取模,默认为2

    empty(可选):如果数据为空显示的字符串

原文地址:https://www.cnblogs.com/cl94/p/9009794.html