Use twig to assemble data to visualize the data structure

Source: Internet
Author: User
: This article describes how to use twig to assemble data to visualize the data structure. For more information about PHP tutorials, see. Business Scenario:
Third-party platforms need to assemble a large amount of JSON data when loading more images using ajax on pages. if the data format is written into the code, the data structure is not clear enough, if the data structure changes, the changes will be troublesome. Therefore, the twig template is used to assemble the data so that the data structure is clear and visible. in the future, as long as the data structure changes are passed in to the twig template, and modify the data structure of twig.
** 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 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 % }{# here {}#}{}{% endif %}], "file_cnt": {"total ": {fileCnt. total }}, "img_cnt": {fileCnt. imgCnt }}...... }, "Is_upload_cdn_ OK": {appMsgInfo. isUploadCdnOK }}, "search_cnt" :{{ appMsgInfo. searchCnt }}}}

Action:

/*** Load more images and texts * @ Template () * @ param Request $ request * @ Route ("/xxx, name =" xxx ") * @ return array */publicfunctiongetWeixinNewsesHtmlAction (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, "appMsgSent Cnt "=> 0); $ appMsgInfo = array (" isUploadCdnOK "=> 0," searchCnt "=> empty ($ query )? 0: $ count); returnarray ("baseResp" => $ baseResp, "fileCnt" => $ fileCnt, "weixinNewses" => $ weixinNewses, "appMsgInfo" = >$ appMsgInfo ));}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above describes how to use twig to assemble data to visualize the data structure, including some content, and hope to help friends who are interested in PHP tutorials.

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.