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:
<code> { <span>"base_resp"</span>: { <span>"ret"</span>:{{ baseResp<span>.ret</span> }}, <span>"err_msg"</span>:<span>"{{ baseResp.errMsg }}"</span> }, <span>"app_msg_info"</span>: { <span>"item"</span>: [ {% if weixinNewses is not empty %} {% for weixinNews <span>in</span> weixinNewses %} { <span>"seq"</span>:{{ weixinNews<span>.id</span> }}, <span>"app_id"</span>:{{ weixinNews<span>.id</span> }}, <span>"file_id"</span>:{{ weixinNews<span>.id</span> }}, <span>"title"</span>:<span>"{{ weixinNews.title }}"</span>, <span>"digest"</span>:<span>"{{ weixinNews.digest }}"</span>, <span>"create_time"</span>:<span>"{{ weixinNews.createdTime|datetimeToSecond }}"</span>, <span>"multi_item"</span>: [ { <span>"seq"</span>:{{ weixinNews<span>.id</span> }}, <span>"cover"</span>:<span>"{{ weixinNews.thumbMediaUrl }}"</span> …… } ], <span>"content_url"</span>:<span>"{{ weixinNews.detailUrl }}"</span>, <span>"img_url"</span>:<span>"{{ weixinNews.thumbMediaUrl }}"</span>, <span>"author"</span>:<span>"{{ weixinNews.author }}"</span>, <span>"show_cover_pic"</span>:<span>1</span>, <span>"update_time"</span>:<span>"{{ weixinNews.updatedTime|datetimeToSecond }}"</span> }, {% endfor %} {<span>#这里要有{}#}</span> {} {% endif %} ], <span>"file_cnt"</span>: { <span>"total"</span>:{{ fileCnt<span>.total</span> }}, <span>"img_cnt"</span>:{{ fileCnt<span>.imgCnt</span> }} …… }, <span>"is_upload_cdn_ok"</span>:{{ appMsgInfo<span>.isUploadCdnOK</span> }}, <span>"search_cnt"</span>:{{ appMsgInfo<span>.searchCnt</span> }} } }</code>
action:
<code><span>/** * 加载更多图文 *<span> @Template</span>() *<span> @param</span> Request $request *<span> @Route</span>("/xxx,name="xxx") *<span> @return</span> array */</span><span>public</span><span><span>function</span><span>getWeixinNewsesHtmlAction</span><span>(Request <span>$request</span>)</span>{</span><span>$weixinNewses</span>=xxx;<span>//微信图文信息</span><span>$baseResp</span> = <span>array</span>(<span>"ret"</span>=><span>$ret</span>,<span>"errMsg"</span>=><span>$errMsg</span>); <span>$fileCnt</span> = <span>array</span>( <span>"total"</span>=><span>$count</span>, <span>"imgCnt"</span>=><span>0</span>, <span>"voiceCnt"</span>=><span>0</span>, <span>"videoCnt"</span>=><span>0</span>, <span>"appMsgCnt"</span>=><span>$count</span>, <span>"commondityMsgCnt"</span>=><span>0</span>, <span>"videoMsgCnt"</span>=><span>0</span>, <span>"shortVideoCnt"</span>=><span>0</span>, <span>"appMsgSentCnt"</span>=><span>0</span> ); <span>$appMsgInfo</span> = <span>array</span>(<span>"isUploadCdnOK"</span>=><span>0</span>, <span>"searchCnt"</span>=><span>empty</span>(<span>$query</span>)?<span>0</span>:<span>$count</span> ); <span>return</span><span>array</span>((<span>"baseResp"</span>=><span>$baseResp</span>,<span>"fileCnt"</span>=><span>$fileCnt</span>,<span>"weixinNewses"</span>=><span>$weixinNewses</span>,<span>"appMsgInfo"</span>=><span>$appMsgInfo</span>)); }</code>
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.