CodeIgniter or PHP Equivalent of Rails Partials an

2019-02-05 05:07发布

In CodeIgniter, or core PHP; is there an equivalent of Rails's view partials and templates?

A partial would let me render another view fragment inside my view. I could have a common navbar.php view that I could point to the inside my homepage.php view. Templates would define the overall shell of an HTML page in one place, and let each view just fill in the body.

The closest thing I could find in the CodeIgniter documentation was Loading multiple views, where several views are rendered sequentially in the controller. It seems strange to be dictating the visual look of my page inside the controller. (i.e. to move the navbar my designer would have to edit the controller).

I've been searching on stackoverflow for a PHP way to accomplish this. I have found this page, which talks about simulating partials with ob_start. Is that the recommended approach inside CodeIgniter?

11条回答
一纸荒年 Trace。
2楼-- · 2019-02-05 05:42

Try out Ocular - http://codeigniter.com/wiki/Ocular_Layout_Library/ - its a rails inspired templating library.

查看更多
贼婆χ
3楼-- · 2019-02-05 05:43

In PHP you'd use include

查看更多
男人必须洒脱
4楼-- · 2019-02-05 05:44

You can check it, for partials and template

    http://code.google.com/p/ocular/wiki/Introduction
查看更多
乱世女痞
5楼-- · 2019-02-05 05:45

My recently released Template library works in this way.

That template library also plays nicely with my Dwoo implementation which will give your views much more power.

查看更多
做自己的国王
6楼-- · 2019-02-05 05:47

This is unfortunately not CodeIgniter specific at all, however I suggest you to have a look at Savant3 template system. It allows you to render the template to a string. Then you can simply stuck it to wherever you wish.

Maybe there's something like that in CodeIgniter?

I can think of a way to add a facility when rendering the view, to render the template, and then all the sub-template it contains.

查看更多
登录 后发表回答