欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

使用twig来组装数据使数据结构可视化

程序员文章站 2022-03-21 09:52:49
...
业务场景:
第三方平台实现微信图文,在页面上用ajax加载更多图文时需要组装大量的JSON数据,如果把数据的格式写到代码里面会使数据的结构不够清晰,如果数据结构变动那么改动就会比较麻烦,所以利用twig模板来组装数据,使数据结构清晰可视,以后数据结构变动只要修改传入到twig模板的数,以及修改twig的数据结构。
**项目使用的框架:**symfony

twig:


{
    "base_resp":
    {
        "ret":{{ baseResp.ret }},
        "err_msg":"{{ baseResp.errMsg }}"
    },
    "app_msg_info":
    {
        "item":
        [
            {% if weixinNewses is not empty %}
                {% for weixinNews in weixinNewses %}
                    {
                        "seq":{{ weixinNews.id }},
                        "app_id":{{ weixinNews.id }},
                        "file_id":{{ weixinNews.id }},
                        "title":"{{ weixinNews.title }}",
                        "digest":"{{ weixinNews.digest }}",
                        "create_time":"{{ weixinNews.createdTime|datetimeToSecond }}",
                        "multi_item":
                        [
                            {
                                "seq":{{ weixinNews.id }},
                                "cover":"{{ weixinNews.thumbMediaUrl }}"
                                ……
                            }
                        ],
                        "content_url":"{{ weixinNews.detailUrl }}",
                        "img_url":"{{ weixinNews.thumbMediaUrl }}",
                        "author":"{{ weixinNews.author }}",
                        "show_cover_pic":1,
                        "update_time":"{{ weixinNews.updatedTime|datetimeToSecond  }}"
                    },
                {% endfor %}
                {#这里要有{}#}
                {}
            {% endif %}
        ],
        "file_cnt":
        {
            "total":{{ fileCnt.total }},
            "img_cnt":{{ fileCnt.imgCnt }}
            ……
        },
        "is_upload_cdn_ok":{{ appMsgInfo.isUploadCdnOK }},
        "search_cnt":{{ appMsgInfo.searchCnt }}
    }
}

action:

/**
     * 加载更多图文
     * @Template()
     * @param Request $request
     * @Route("/xxx,name="xxx")
     * @return array
     */publicfunctiongetWeixinNewsesHtmlAction(Request $request){$weixinNewses=xxx;//微信图文信息$baseResp = array("ret"=>$ret,"errMsg"=>$errMsg);
            $fileCnt = array(
                "total"=>$count,
                "imgCnt"=>0,
                "voiceCnt"=>0,
                "videoCnt"=>0,
                "appMsgCnt"=>$count,
                "commondityMsgCnt"=>0,
                "videoMsgCnt"=>0,
                "shortVideoCnt"=>0,
                "appMsgSentCnt"=>0
            );
            $appMsgInfo = array("isUploadCdnOK"=>0,
                "searchCnt"=>empty($query)?0:$count
            );
            returnarray(("baseResp"=>$baseResp,"fileCnt"=>$fileCnt,"weixinNewses"=>$weixinNewses,"appMsgInfo"=>$appMsgInfo));
    }

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了使用twig来组装数据使数据结构可视化,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

使用twig来组装数据使数据结构可视化

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频