Pass data to a partial inside a loop |
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
(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. I would change this world, but God doesn't give me the source.
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
(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']) ?> I would change this world, but God doesn't give me the source.
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
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. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Can you please explain how it works?
InsiteFXDo what I do so all views will see the data. |
Welcome Guest, Not a member yet? Register Sign In |