您的位置:首页 > 理论基础 > 数据结构算法

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

2015-07-29 14:35 736 查看
业务场景

第三方平台实现微信图文,在页面上用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));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  symfony twig php