How to set global layout data? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: How to set global layout data? (/showthread.php?tid=80402) |
How to set global layout data? - forumz - 10-27-2021 Is there a way to set layout data in a controller, so that the data is available every time when the layout is rendered? CodeIgniter 3 had this functionality available and it worked well. The new layout system is more like one Django uses, and I absolutely hate it. Maybe I'm just missing something here ... Help on this greatly appreciated! RE: How to set global layout data? - InsiteFX - 10-28-2021 I createed a method in the BaseController to merge a global array with the Controllers data array. BaseController: PHP Code: <?php Read the comments. Some times you will need to create an empty view_data.php view file to call first to load global data. RE: How to set global layout data? - forumz - 10-28-2021 Thank for the reply. I looked closer at how templates are rendered in the View class of CI4 and decided not to use them at all. Instead I just render views. In order to pass common data to views like header or footer I added a simple static variable in BaseController class. The variable is just an associative array. It contains keys and values. PHP Code: public static $globals = []; If I need to add some global data somewhere, like in the BaseController, I just create a key and assign a value. PHP Code: self::$globals['key'] = 'value'; Then in my view: PHP Code: esc(\App\Controller\BaseController::$globals['key']); |