Welcome Guest, Not a member yet? Register   Sign In
How do I create a nested view within a nested view?
#1

I've been stuck on this for a few days now and can't seem to put the final piece of the puzzle in place.  I've created a page of nested views that I populate with vars obtained from calls to models from the controller - using the TRUE Parameter to save the views as vars. I successfully reference these vars when loading a final page view. Works great and has simplified my first pass at this which was one huge view.


One of the six partial views is itself a table of eight rows.  I have at this point  built the logic for this list of smaller tables into a single view assigned to a single var. I can load it successfully as the fifth of the six partial page views.

What Id really like to do now is separate out the eight rows of each small table into separate view files that I can easily work on. I've studied the posts here that apply - especially "Business Logic in Nested Views" at https://forum.codeigniter.com/thread-348...sted+views - which it seems might hold the secret. I've also read the CI User Guide on views many times now. And many Stack Overflow possible answers. But from all that I still can't figure out a strategy for this last step.

This is my first CI project. I think I'm way out in space on this and that there's a a simple answer I'm totally missing. Any suggestions pointing me in a promising direction would be appreciated.
Reply
Reply
#3

If you have to open a view in a view you're doing something very wrong. You can always just do an include_once('file/location/..'); since im assuming your code is bad it would blend right in.
Reply
#4

I often open views within views. I see nothing wrong with a view that looks something like:

View example
Code:
<h1><?php echo $user_name; ?></h1>

$this->load->view('partials/profile_view');

$this->load->view('partials/forms/add_profile_info_view');

$this->load->view('partials/adverts/upgrade_account_view');

<?php foreach ($options as $option) { ?>
     $this->load->view('partials/options/'.$option['option_url']);
<?php } ?>

As long as the controller is collecting all the data, libraries or models are performing all the business logic and database calls seperately, and the data is loaded into the view vars, every view will have access to all the data. Now when I want to change one of the options(say) from the many options available, I just alter one view file for that options partial.

I would use a template library of my own making to add header, footer, sidebar etc page layout bits and bobs. But in this example profile page many partials could be called.

I find this is very efficient. In a todo list I did recently each row of the table of todo's was calling a partial task view to display the task. In a recent calendar each day was calling a day view that did the layout of the day contents. In a shopping cart I did recently, each item in the basket was displayed with a basket_item view.

Efficient, dry, perfectly acceptable and still works incredibly quickly. It is normally my js and css that slows a page load down, never CI doing the work in the background.

Hope that helps,

Paul
Reply
#5

(This post was last modified: 09-26-2017, 09:34 PM by codeguy.)

Thanks for the encouraging reply. But I am confused that I see no data being transferred to your views. Like I'd expect something like . .

$this->load->view('partials/profile_view',$profile_data);

. .  perhaps preceded by a line like . .

$modRetArr['profile_data']=$this->M_profiles->get_profiles_data($params);

How do the views get the data? Is what you posted a full controller?

A few hrs later ***************** after studying your reply several times I wonder if perhaps the view load stmts did not require data because it was loaded into the views earlier. If so could you show that part possibly? Or maybe you thought I understood that part already and didn't need to show it. Or maybe they didn't use any dynamic data. I'm probably missing something simple and fundamental. Thanks for your patience.
Reply
#6

You can use an undocumented CodeIgniter method:

PHP Code:
$data = array();

$data['title' 'My Title';
$data['header'] = 'My Header';
        
/**
 * CodeIgniter Undocumented vars method.
 * This method caches all the data global
 * to the views.
 */
$this->load->vars($data);
        
$this->load->view('Your_View'); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

@PaulD, You described your example as a view. I read it several times but my brain did not accept it. What confused me was you had four "$this->load->view() stmts in the example. My basic understanding for all the time I've spent learning CI is that a view is an html file or fragment. And "$this->load->view()" is a method of the CI controller class and therefore can only be invoked from within a controller. And so my brain told me I was looking at a controller - even though you clearly said it was an example of as view. If I can call the load method from within an html file (view) as your example shows then I think that makes the whole problem disappear. I'll try that.
Reply
#8

@InsiteFX, I did come across that while trying to figure this out and it seemed it might be useful. I'll take another look. Thanks for your help.
Reply
#9

(09-27-2017, 06:58 AM)codeguy Wrote: @PaulD, You described your example as a view. I read it several times but my brain did not accept it. What confused me was you had four "$this->load->view() stmts in the example. My basic understanding for all the time I've spent learning CI is that a view is an html file or fragment. And "$this->load->view()" is a method of the CI controller class and therefore can only be invoked from within a controller. And so my brain told me I was looking at a controller - even though you clearly said it was an example of as view. If I can call the load method from within an html file (view) as your example shows then I think that makes the whole problem disappear. I'll try that.

Hi,

Yes you can load views from within views. It really is that simple. As for the example I gave I was showing you can have lots of them in one view file, or even a loop to load various views if it helps, like for each table row in a table.

Paul.
Reply
#10

(09-26-2017, 10:43 PM)InsiteFX Wrote: You can use an undocumented CodeIgniter method:

PHP Code:
$data = array();

$data['title' 'My Title';
$data['header'] = 'My Header';
 
/**
 * CodeIgniter Undocumented vars method.
 * This method caches all the data global
 * to the views.
 */
$this->load->vars($data);
 
$this->load->view('Your_View'); 

Not busting your chops but the method is documented HERE.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB