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

(This post was last modified: 09-28-2017, 10:54 AM by PaulD.)

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');

class 
Template_library {

 
     public $page_data = array();
 
     
      public 
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.
 
           $this->CI->load->view('common/header_view'$this->page_data);
 
           $this->CI->load->view('common/navbar_view');
 
           $this->CI->load->view($template);
 
           $this->CI->load->view('common/member_footer_view');
 
     }
 
     
      
// setter to add data to page_data from controller
 
     public function set_page_data($name$data NULL
 
     {
 
           $this->page_data[$name] = $data;
 
           return;
 
     }
 
     



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

class 
Home extends CI_Controller {
 
     
      public 
function index()
 
     {
 
           // basic layout data - just example of setting some data
 
           $this->template_library->set_page_data('meta_title''home');
 
           $this->template_library->set_page_data('page_subtitle''Home');
 
           $this->template_library->set_page_data('page_title''Welcome');

 
           // dynamic data collection
 
           $this->template_library->set_page_data('articles'$this->blog_library->get_latest_articles());
 
           
            
// render view
 
           $this->template_library->render_page('home_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.
Reply


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



Theme © iAndrew 2016 - Forum software by © MyBB