Question

I wonder what is the correct approach to achieve this? I have an "overview" of products and other stuff it's in the "view"-file of the controller. I want to show product-items in a list on an overview page, but in other templates as well (same html-elements but with different values for each product). I don't want to type same thing twice and want to easily maintain the code.

I have this file (views/profile/overview.php) it's basically a for-each loop listing of products - something like:

foreach($items as $item) {
    echo $item->name;
    echo $item->creation_date;
}

Is there a way to put

 echo $item->name;
 echo $item->creation_date;

in a single partial view (like product_item) and load that dynamically?

I've tried: (does not work because $item is undefined in product_item)

foreach($items as $item) {
        $this->load->view('/profile/partials/product_item');
}

Try 2: (didn't work either. I don't know why - because $item should be passed to that view?)

foreach($items as $item) {
       $this->load->view('/profile/partials/product_item', $item);
}

Try 3: (same as try 2)

foreach($items as $item) {
       $x = $this->load->view('/profile/partials/product_item', $item);
       echo $x;
}

Try 4 (I've tried to do this in controller (I then just wanted to echo out $product_item into the overview-template)

$this->view_data['product_item'] = $this->load->view('profile/partials/product_item', $this->view_data, true);

I have to mention that product_item and it's html is far more code than this, I just wanted to describe my issue. If you need more, please tell me :-)

Was it helpful?

Solution

load->view() takes an associative array or an object as a parameter, so this should work:

foreach($items as $item) {
    $this->load->view('/profile/partials/product_item', array('item' => $item));
}

After that, you can use:

echo $item->name;
echo $item->creation_date;

in your view.


Remember, every array key of the array/object that you pass to your load view function becomes a variable in the view. So, if you send:

$data['hey'] = 'there';

In your view, you'll have the variable $hey which has the value 'there'.

OTHER TIPS

To Build on what @Shomz as just said

Master Template

views/master/template.php

Say you load in your Product Controller View

<section role="content">
    <?php echo $this->load->view($view); ?>
</section>

But What if you then want to load some more information from the Product Controller, in say the sidebar?


Well you can do so from the Product Controller, and simple do a Conditional to check if the variable was sent back from the Controller.

<aside role="sidebar">
    <section>
         //Normal sidebar stuff here, nothing to worry about
    </section>

    <?php if($sidebar) : ?> //Did ANY Controller send a Sidebar Variable ?
    <section >
       <?php echo $this->load->view($sidebar); ?>
    </section>
    <?php endif; ?>
</aside>

Controller

public function someMethod()
{
    $this->template = 'master/template'; //views/master/template

    return $this->load->view($this->template, array(
          'view'   =>  'products/index' // the $view var
          'sidebar' => $this->load->view('partials/sidebar', array('product_data'=>$model), true) //$sidebar var buffered with dynamic data
    ));
}

I realise that this may be a response to a dated question; but in addition to @Philip's answer, the CodeIgniter Template Parser Class can be used to prevent the 'contamination' of the view files with PHP code.

An example with the iteration controlled in the controller, using a view fragment:

$temp = '';
$template1 = '<li><a href="{link}">{title}</a></li>';
$data1 = array(
        array('title' => 'First Link', 'link' => '/first'),
        array('title' => 'Second Link', 'link' => '/second'),
);

foreach ($data1 as $menuitem)
{
        $temp .= $this->parser->parse_string($template1, $menuitem, TRUE);
}

$template = '<ul>{menuitems}</ul>';
$data = array(
        'menuitems' => $temp
);
$this->parser->parse_string($template, $data);

Result:

<ul>
        <li><a href="/first">First Link</a></li>
        <li><a href="/second">Second Link</a></li>
</ul>

A further tutorial is avaialble at Introduction to Templating and Dynamic Views with Codeigniter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top