CodeIgniter Forums
How to encapsulate view-components? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: How to encapsulate view-components? (/showthread.php?tid=74231)



How to encapsulate view-components? - jellix - 08-30-2019

Hi,

I have two different views, both of them should display different tables. But both tables should be embedded within a common „tableFrame“ that has some JS-functionality.

Usually I would do that with different subTableClasses that extend a superTableClass (classical OOP).

Is there a way to say something like:
<div class=„tableFrame“><?php get_view_part(...)?>
<table> bla ... </table>
</div>

U know what I mean? :-)

Thanks in advance!

jellix


RE: How to encapsulate view-components? - ignitedcms - 09-01-2019

I'd probably write my own html helper() function.


RE: How to encapsulate view-components? - Wouter60 - 09-01-2019

Maybe this will help.
Put this code in both views that should display the table:
PHP Code:
<div class="tableFrame“>
<?php 
$this->load->view('embedded_viewfile');?>
</div> 
Where 'embedded_viewfile' is the name of the view that you want to embed.

You probably need to pass variables (the records) to the embedded view.
If you do that from the controller, you can make these variables global.
Before you load the 'container' views, do this:
PHP Code:
$data['records'] = ... // an array with records from a database table, collected by a query (e.g. in a model)
$this->load->vars($data['records']); 

In the embedded view, the $records array is available for displaying the data.


RE: How to encapsulate view-components? - jellix - 09-01-2019

Thanks to both of you.
Now I understood the mechanic of loading views within views and the helper-concept is also very important. Just looking at it :-)