: 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.