CodeIgniter Forums
Best way create layouts library vs extend CI_Controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Best way create layouts library vs extend CI_Controller (/showthread.php?tid=72561)



Best way create layouts library vs extend CI_Controller - asalves - 01-06-2019

Hello guys

I'm start working with CI4 for testing propose and prepare the future migrations on my company website.

I have one simply question, Layout?

In the past i used the avenir approach (https://avenir.ro/create-cms-using-codeigniter-3/create-template-for-admin-area/), I Public_Controller with 1 layout and have Backoffice_Controller with another layout. It works fine for me.

But I also see another approach, the Library approach, (some old stuff here https://github.com/philsturgeon/codeigniter-template)

I recently find another one the Hook approach, (https://stackoverflow.com/questions/2956836/codeigniter-and-layouts)

So what is your opinion, what is the best approach for creating a layout system for CI4?


I like the extend My_controller approach, but I really confuse with the best way to do thing in CI4


RE: Best way create layouts library vs extend CI_Controller - natanfelles - 01-06-2019

Some time ago, one approach I used was to create a library "App\Libraries\Theme".

Then, use it as a service. Or, setting in a property of an abstract BaseController.

It could be used like "$this->theme->render('view')", for example.

To expedite, it has a "theme config". That could be set in the BaseController or other site area controllers that extends it. Then, many themes are possible. The "render" method automatically renders the view in the theme path, like "APPPATH . 'Views/theme-x/view-name.php'".

The data of head, header, footer, etc, are passed with a setData() method. That allow pre-set array keys in the BaseController, like an User object created according the Session user_id.

Hope it help.


RE: Best way create layouts library vs extend CI_Controller - kilishan - 01-06-2019

In the past I've created a ThemedController that any controller could extends that provided a simple template system.

And, in CI4, I've even managed to extend the View library and provide some traditional template-like methods, including extending layouts, sections for dropping content into, etc. The secret sauce there is that you must remember when the views are rendered, they are in the scope of the View library that's rendering them. So you can add additional methods to your overridden class, and add whatever functionality you want.