Use twig to assemble data and visualize data structures

WBOY
Release: 2016-08-08 09:19:45
Original
1358 people have browsed it

Business scenario:
A third-party platform implements WeChat images and texts. When using ajax to load more images and texts on the page, a large amount of JSON data needs to be assembled. If the data format is written into the code, the data structure will not be clear enough. If the data structure changes, the changes will be difficult. It will be more troublesome, so use twig template to assemble the data to make the data structure clearly visible. In the future, if the data structure changes, you only need to modify the number passed to the twig template and modify the data structure of twig.
**Framework used in the project:**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 }} } }
Copy after login

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)); }
Copy after login

Copyright statement: This article is an original article by the blogger and may not be reproduced without the permission of the blogger.

The above introduces the use of twig to assemble data to visualize the data structure, including aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
gt
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!