![]() |
Pass data to a partial inside a loop - 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: Pass data to a partial inside a loop (/showthread.php?tid=76800) |
Pass data to a partial inside a loop - nneves - 06-21-2020 Hi This is the first time I'm using CI and I have a questiona about getting data inside a partial within a loop. On the controller I call the view with an array of data: PHP Code: return view('numero', $data); In the main view I have: PHP Code: <?php foreach ($items as $item) : ?> In the partial I have: PHP Code: <?= $item['name']; ?> and I get an error saying "Undefined variable: item" How am I able to make this work? Thanks RE: Pass data to a partial inside a loop - Digital_Wolf - 06-21-2020 (06-21-2020, 04:46 AM)nneves Wrote: Hi Hello ! This question has already been answered in the user manual. Carefully study the code blocks that were provided as an example, especially pay attention to the block with the code that is called in the view itself on line No. 9, here's the record: PHP Code: <?php foreach ($todo_list as $item):?> Read the: User_Guide#Creating-Loops p.s. I have not coded for a long time and therefore I can be mistaken in my last sentence. P.S. Sorry my English. RE: Pass data to a partial inside a loop - nneves - 06-22-2020 Hi @Digital_Wolf Thanks for your feedback. The interaction works when everything is inside the main template and stop working when I place the variable inside a partial template. I think that loops only work if they are in the same template or partial, I have loops inside partials but the stop working when the loop includes the partial like I wrote in the first post. Thanks RE: Pass data to a partial inside a loop - Digital_Wolf - 06-22-2020 (06-22-2020, 01:32 AM)nneves Wrote: Hi @Digital_WolfThe documentation says little about the "$this->include("partials/item")" method, and I myself haven’t used it since I don’t have duplicate code, but the same documentation mentions that you can transfer data exactly the same way as in controllers, that is, after the line with the file name "'partials/item'", you can specify the data you want to transfer after the comma, it will look something like this (although I'm not sure): PHP Code: <?= this->include("partials/item", $item['name']) ?> RE: Pass data to a partial inside a loop - ralomach - 10-26-2020 I had the same problem. If you analize the View class at Codeigniter system folder, View folder, View.php (namespace: Codeigniter\View\View), the render method only accepts an array with options 'cache' and 'cache_name'. So I made this to accept passing data to my included view: Find this code on the file: Code: // Was it cached? After that, I include my own code: Code: // Has partial data? So, your code would work as: Code: <?php foreach ($items as $item) : ?> Ps.: compact('item') is the same as 'item' => $item RE: Pass data to a partial inside a loop - InsiteFX - 10-26-2020 Do what I do so all views will see the data. Create a dummy view with nothing in it, data_view.php leave it empty. Then load this view as the first one. PHP Code: echo view('data_view', $data); Also don't include the partial view load like a regular view. PHP Code: <?= view('your_partial_view');?> When you use include it will not see the data variables. RE: Pass data to a partial inside a loop - clsmedia - 05-30-2022 Can you please explain how it works? InsiteFXDo what I do so all views will see the data. |