title: Is there a way to include dynamically html in partial views in CodeIgniter?
body: <p>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 (<em>same html-elements but with different values for each product</em>). I don't want to type same thing twice and want to easily maintain the code.</p>
  
  <p>I have this file <em>(views/profile/overview.php</em>) it's basically a for-each loop listing of products - something like:</p>
  
  <pre><code>foreach($items as $item) {
      echo $item-&gt;name;
      echo $item-&gt;creation_date;
  }
  </code></pre>
  
  <p><strong>Is there a way to put</strong> </p>
  
  <pre><code> echo $item-&gt;name;
   echo $item-&gt;creation_date;
  </code></pre>
  
  <p><strong>in a single partial view</strong> (like product_item) and load that dynamically?</p>
  
  <p>I've tried: (does not work because $item is undefined in product_item)</p>
  
  <pre><code>foreach($items as $item) {
          $this-&gt;load-&gt;view('/profile/partials/product_item');
  }
  </code></pre>
  
  <p><strong>Try 2:</strong> (didn't work either. I don't know why - because $item should be passed to that view?)</p>
  
  <pre><code>foreach($items as $item) {
         $this-&gt;load-&gt;view('/profile/partials/product_item', $item);
  }
  </code></pre>
  
  <p><strong>Try 3:</strong> (same as try 2)</p>
  
  <pre><code>foreach($items as $item) {
         $x = $this-&gt;load-&gt;view('/profile/partials/product_item', $item);
         echo $x;
  }
  </code></pre>
  
  <p><strong>Try 4</strong> (I've tried to do this in controller (I then just wanted to echo out <code>$product_item</code> into the overview-template)</p>
  
  <pre><code>$this-&gt;view_data['product_item'] = $this-&gt;load-&gt;view('profile/partials/product_item', $this-&gt;view_data, true);
  </code></pre>
  
  <p>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 :-) </p>