首页 > 后端开发 > php教程 > 使用twig来组装数据使数据结构可视化

使用twig来组装数据使数据结构可视化

WBOY
发布: 2016-08-08 09:19:45
原创
1430 人浏览过

业务场景:
第三方平台实现微信图文,在页面上用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教程有兴趣的朋友有所帮助。

相关标签:
gt
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板