douzhe9075 2015-08-21 11:30
浏览 31
已采纳

哪个在Symfony2中更好:渲染控制器或包含模板?

I develop university site with Symfony2. Besides I'm almost newbie in Symfony.

Index page consists of several blocks: news, events, photos, etc. All objects are fetched from webservices, so no DB is used directly.

I wonder which is better:

  1. Fetch all news,photos,.. objects in DefaultController:indexAction and pass them to index.html.twig as arguments
  2. Call render(controller(News:block)) in index.html.twig. And NewsController in turn fetches objects and renders news_block.html.twig.
  3. Some other way?.. I actually use the second way but I think it is not good.

Please give me advice.

  • 写回答

1条回答 默认 最新

  • dongqiaogouk86049 2015-08-21 11:55
    关注

    There cannot be a perfect obsolete answer for this question. Both ways are equally standard ways of coding in Symfony. Its a decision based approach which matters a lot.

    1. Fetch all news,photos,.. objects in DefaultController:indexAction and pass them to index.html.twig as arguments -> when the data you are displaying/loading for the page is relatively less or belongs to one single module/feature. This way you have a simple process - route -> controller -> twig. Simple but sweet way of coding.

    2. Call render(controller(News:block)) in index.html.twig. And NewsController in turn fetches objects and renders news_block.html.twig. -> Use this approach when your twig/page is rendering a lot of components or requires a lot of heavy lifting in one action. This will help you a lot in maintaining the code, as each part of your twig is rendered seperately using seperate controllers. Plus, this will also improve the site UX performance a little bit, because the twig is initially rendered with bare minimum information and then each individual component renders itself seperately using own controllers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载