• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I create a nested view within a nested view?

Just saw your latest post and I have to say I do not like the approach. It looks like you are loading all your views into vars, and then outputting them in a 'page_master' type view.

What about something like this:

Template Library: libraries/Template_library.php
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Template_library {

     public $page_data = array();
function __construct()
           $this->CI =& get_instance();

     // render a page view
     public function render_page($template)
           // render a page - this can be as complex as you like of course, here I just output some views.
// setter to add data to page_data from controller
     public function set_page_data($name$data NULL
           $this->page_data[$name] = $data;

Controller controllers/Home.php
PHP Code:
('BASEPATH') OR exit('No direct script access allowed');

Home extends CI_Controller {
function index()
           // basic layout data - just example of setting some data

           // dynamic data collection
// render view

Because the template library loads all the data into the first view, it is available in all subsequent views. So the home view file might load partials/show_blog_articles_view within a loop through that data. (After checking of course that there are some to loop through).

No page views are loaded into memory. All view output is done at the end of all the other processing. The controller just collects the data and sets the views. The libraries (in this case blog_library, collects from the database via a blog model, cleans and validates the output (ie is a blog post public etc).

All the view data is available to all the views called, however many view partials I want, because all the data is loaded into the first view. There is then no chance of conflicts or overwriting etc.

Should I want to change my page layout, I simply alter my template library or any associated views.

I hope that helps.

PS examples given are simple examples to get the point across.

PPS I should just add that CI does not tell you how to do it, so whatever suits you is fine. There is no right answer here. Whatever you are comfortable with. For a long time I just loaded several views at the end of each controller. Part of the beauty of CI is you can do it in whatever way you want.

Messages In This Thread
RE: How do I create a nested view within a nested view? - by PaulD - 09-28-2017, 10:50 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.