使用twig来组装数据使数据结构可视化
2015-07-29 14:35
736 查看
业务场景:
第三方平台实现微信图文,在页面上用ajax加载更多图文时需要组装大量的JSON数据,如果把数据的格式写到代码里面会使数据的结构不够清晰,如果数据结构变动那么改动就会比较麻烦,所以利用twig模板来组装数据,使数据结构清晰可视,以后数据结构变动只要修改传入到twig模板的数,以及修改twig的数据结构。
**项目使用的框架:**symfony
twig:
action:
第三方平台实现微信图文,在页面上用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)); }
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Symfony事件类型
- Php Installing An Expansion
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- apache 环境下 php 的配置注意事项
- ASP.NET、ASP、PHP、JSP之间有什么区别?
- PHP VBS JS 函数 对照表
- C语言实现的统计php代码行数功能源码(支持文件夹、多目录)