doujiao3074 2010-04-14 20:14
浏览 58
已采纳

CodeIgniter - 在网页上创建“块”元素的正确方法

I've been searching for a solutions for this problem a while but haven't seen any "valid mvc" solution for this. I hope I can explain my problem clearly enough for you guys.

  • I need to create a dynamic block of HTML on my website. (eg. a block containing user's latest blog comments).

  • I have a template view file (a file containing header, content container and a footer) where I need to add some content AND this block element.

The problem is that I don't want to duplicate this block code on every controller. It just feels stupid and I'm sure there's a better way to do this than just duplicating same stuff all over again on all the controller files?

I can add view inside another view just fine, but what bugs me is how to actually generate that dynamic content to this block's view-file. I can't call controller from view file, controller from controller filem or model from view file because what I understand that just isn't the "mvc" way?

Anyone got any tricks or tips for this?

  • 写回答

3条回答 默认 最新

  • dongzhilian0188 2010-04-14 22:47
    关注

    I ran into the same problem. I didn't want to duplicate this common logic across my action methods. I was moving this logic up to the base controller but it felt a bit clunky.

    I eventually stumbled upon the HMVC extension to CodeIgniter. Its a really nice way of generating partial views. You create a module responsible for generating the partial. The module is coded in an MVC way, just like you would code your pages. This module can then be included in your view.

    If you've used ASP.NET MVC before, its similar to using Html.RenderAction.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题