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.

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

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条