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.