Use twig to assemble data to visualize the structure

Source: Internet
Author: User
Business Scenario:
Third-party platform implementation of graphics and text, on the page with Ajax loading more graphics and text need to assemble a large number of JSON data, if the format of data written into the code will make the structure of the data is not clear, if the data structure changes will be more cumbersome, so the use of twig template to assemble data, make the structure clearly visible , the data structure changes in the future as long as the number of changes passed into the twig template, as well as modify the twig data structure.
* * Framework used by the project: **symfony

Twig

{"Base_resp":    {"ret": {{Baseresp. RET}},"Err_msg":' {{baseresp.errmsg}} '},"App_msg_info":    {"Item": [{% If weixinnewses is not empty%} {% for weixinnewsinchWeixinnewses%} {"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

/** * Load More Graphics * @Template() * @param Request $request * @Route("/xxx,name=" x XX ") * @return array * / Public functiongetweixinnewseshtmlaction(Request $request){$weixinNewses=xxx;//Graphic information$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)); }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the use of twig to assemble the data structure visualization, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.