![]() |
Clarification how to use view_cell, layouts or views - 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: Clarification how to use view_cell, layouts or views (/showthread.php?tid=76074) |
Clarification how to use view_cell, layouts or views - elserra - 04-12-2020 Hi everybody, I've been struggling on how or when to use which type of view. I understand the differences and I think I know how to use them but it is unclear to me what the benefits are. Let's say I have the structure as follows: ------------------- | Navbar | ------------------- | Main | ------------------- | Footer | ------------------- What is the difference between using those structures? 1: 3 php files echo view(navbar.php, $data1) echo view(main.php, $data2) echo view(footer.php, $data1) 2: 1 main.php + view_cells main.php: [...] <body> <?= view_cell('App\Libraries\ViewComponents::getNavbar', '$data1') ?> <?= view_cell('App\Libraries\ViewComponents::getMain', '$data2') ?> <?= view_cell('App\Libraries\ViewComponents::getFooter', '$data1') ?> </body> 3: Sections main.php [...] <body> <?= this->renderSection('page_body') ?> <?= this->renderSection('page_body') ?> <?= this->renderSection('page_footer') ?> </body> RE: Clarification how to use view_cell, layouts or views - Leo - 04-15-2020 Hi, I'm not sure of the "correct" way either - I do it like this: In the controller: echo view('front', $this->data); In view file 'front.php': <!DOCTYPE html> <html> <head><!-- head stuff --></head> <body> <?= $this->include('home') ?> <!-- this points to view file home.php in the app/Views directory <footer><!-- footer stuff --></footer> </body> </html> view file home.php contains: <main> <header><h1>Very neat, and semantically correct HTML stuff</h1></header> <section>....</section> <!-- I also may have a little, hidden form embeded in this file, that is located in app/Views/modules/popform.php, like so: --> <?= $this->include('modules/popform') ?> </main> If someone more senior sees this and cringes please tell me before I write a lot of stuff this way. Or is it good? RE: Clarification how to use view_cell, layouts or views - stlake2011 - 04-15-2020 (04-12-2020, 10:42 PM)elserra Wrote: Hi everybody, Not completely sure myself but I always use the first way you listed as that matches the way I was doing it long before frameworks became a thing. I also find it allows me to have granular control if needed and not having to process/type as much. RE: Clarification how to use view_cell, layouts or views - elserra - 04-15-2020 (04-15-2020, 04:23 AM)Leo Wrote: Hi, I'm not sure of the "correct" way either - I do it like this: Right, that one was missing. What is actually the difference between $this->include() and echo view() ? RE: Clarification how to use view_cell, layouts or views - kilishan - 04-15-2020 All three ways work. It's whatever works best for your workflow. View Cells are a little "heavier" as they have to parse and fire up another class to do the work. I typically think of those as best used for things that have to hit the database and might be on several pages, like a Recent Posts list on a blog, or something like that. Especially since you can easily cache those views so you don't have to hit the database at all. The only real benefit to using $this->renderSection() and the others is to allow for templates that can be shared among all of your page views for different page layouts (like homepage, two-column, three-column, admin, etc). RE: Clarification how to use view_cell, layouts or views - elserra - 04-15-2020 Okokokok I think I get it now. I am developing a blog, that's why I thank you a lo for the heads up with the view cells and caching (although there's not much to put into cache, but for the sake of optimization). RE: Clarification how to use view_cell, layouts or views - Corsari - 04-16-2023 (04-15-2020, 01:32 PM)kilishan Wrote: All three ways work. .... t. Especially since you can easily cache those views so you don't have to hit the database at all. Hello cache sounds great can you kindly hint how to enable the cacheing of such a view cell? Or it will got cached automatically? Also: Navigation bar has much sense in a layout template or as cell to be called from the main template file? For readability could be, but is there something to consider about such approach? Pro / Cons Thank you RE: Clarification how to use view_cell, layouts or views - InsiteFX - 04-16-2023 READ: CodeIgniter 4 User Guide - View Cells - Cell Caching RE: Clarification how to use view_cell, layouts or views - Corsari - 04-16-2023 Thank you InsiteFX One kind hint.. From the link you kindly provided "You can cache the results of the view cell call by passing the number of seconds to cache the data for as the third parameter. This will use the currently configured cache engine." Configured Cache Engine sentence, mean? Thank you RE: Clarification how to use view_cell, layouts or views - kenjis - 04-17-2023 See https://codeigniter.com/user_guide/libraries/caching.html#configuring-the-cache |