Welcome Guest, Not a member yet? Register   Sign In
Pass data to a partial inside a loop
#1

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) : ?>

<?= $item['id']; ?>

<?= $this->include('partials/item'?>

<?php endif ?>

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
Reply
#2

(06-21-2020, 04:46 AM)nneves Wrote: 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) : ?>

<?= $item['id']; ?>

<?= $this->include('partials/item'?>

<?php endif ?>

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

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):?>
in any case, look carefully at how your keys are named in the $ data array. If memory serves me right and you want to iterate over the entire $data array, then you just need to specify this variable instead of your $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.
Reply
#3

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
Reply
#4

(06-22-2020, 01:32 AM)nneves Wrote: 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
The 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']) ?>
and already process the data in the "partials / item" file itself...
I would change this world, but God doesn't give me the source.
Reply
#5
Bug 

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?
if (isset($this->renderVars['options']['cache'])) {
  $this->renderVars['cacheName'] = $this->renderVars['options']['cache_name'] ?? str_replace('.php', '', $this->renderVars['view']);
 
  if ($output = cache($this->renderVars['cacheName'])) {
    $this->logPerformance($this->renderVars['start'], microtime(true), $this->renderVars['view']);
    return $output;
  }
}

After that, I include my own code:

Code:
// Has partial data?
if (isset($this->renderVars['options']['include_data'])) {
  foreach ($this->renderVars['options']['include_data'] as $index => $data) {
    $this->setData([$index => $data]);
  }
}

So, your code would work as:

Code:
<?php foreach ($items as $item) : ?>

<?= $item['id']; ?>

<?= $this->include('partials/item', [
  'include_data' => compact('item')
]) ?>

<?php endif ?>

Ps.: compact('item') is the same as 'item' => $item
Reply
#6

(This post was last modified: 10-26-2020, 04:44 PM by InsiteFX.)

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 )
Reply
#7

Can you please explain how it works? 






InsiteFXDo what I do so all views will see the data.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB