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 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥50 用logisim设计16位单时钟周期cpu
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应
  • ¥15 如何用字典的Key,显示在WPF的xaml中
  • ¥15 weautomate读取Excel表格信息然后填写到网页一直报错,如何解决?
  • ¥15 C#如何在Webview2中获取网页验证码