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

8 篇文章 0 订阅

业务场景
第三方平台实现微信图文,在页面上用ajax加载更多图文时需要组装大量的JSON数据,如果把数据的格式写到代码里面会使数据的结构不够清晰,如果数据结构变动那么改动就会比较麻烦,所以利用twig模板来组装数据,使数据结构清晰可视,以后数据结构变动只要修改传入到twig模板的数,以及修改twig的数据结构。
**项目使用的框架:**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 %}
                {#这里要有{}#}
                {}
            {% endif %}
        ],
        "file_cnt":
        {
            "total":{{ fileCnt.total }},
            "img_cnt":{{ fileCnt.imgCnt }}
            ……
        },
        "is_upload_cdn_ok":{{ appMsgInfo.isUploadCdnOK }},
        "search_cnt":{{ appMsgInfo.searchCnt }}
    }
}

action:

/**
     * 加载更多图文
     * @Template()
     * @param Request $request
     * @Route("/xxx,name="xxx")
     * @return array
     */
    public function getWeixinNewsesHtmlAction(Request $request){
            $weixinNewses=xxx;//微信图文信息
            $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
            );
            return array(("baseResp"=>$baseResp,"fileCnt"=>$fileCnt,"weixinNewses"=>$weixinNewses,"appMsgInfo"=>$appMsgInfo));
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值