首頁 >後端開發 >php教程 >使用twig來組裝資料使資料結構視覺化

使用twig來組裝資料使資料結構視覺化

WBOY
WBOY原創
2016-08-08 09:19:451433瀏覽

業務場景:
第三方平台實作微信圖文,在頁面上用ajax載入更多圖文時需要組裝大量的JSON數據,如果把數據的格式寫到程式碼裡面會使數據的結構不夠清晰,如果數據結構變動那麼改動就會比較麻煩,所以利用twig模板來組裝數據,讓數據結構清晰可視,以後數據結構變動只要修改傳入到twig模板的數,以及修改twig的數據結構。
**專案使用的框架:**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>

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。

以上就介紹了使用twig來組裝資料使資料結構視覺化,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn