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

@Dave friend, in the older versions of the CodeIgniter documentation it was
not documented, thanks for clarifying this, guess I need to sit down this weekend
and read the whole Users Guide again.
What did you Try? What did you Get? What did you Expect?

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

Thanks. I'm trying to implement a strategy from the several good suggestions I've read here. I now have:

Controller and page master view lib_page_view:

PHP Code:
$data3['tunes']=$this->M_tunes->getAllTuneData();
$this->load->vars($data3); //My attempt to cache $tune vars for use in file partials/topper_ssv in case previous line doesn't
$pgDataArr['tunes_sv']=$this->load->view('library_page/tunes_sv',$data3,TRUE);

//last controller stmt:
$this->load->view('library_page/lib_page_v',$pgDataArr);

//lib_page_v:
<?=$header_sv?>
<?=$lib_top_sv?>
<?=$filters_sv?>
<?=$az_jump_sv?>
<?=$tunes_sv?>
<?=$footer_sv?>

In sub view file tunes_sv.php:

PHP Code:
$this->load->view('partials/topper_ssv'); 

In sub, sub view file partials/topper_ssv:

PHP Code:
<div class="panel panel-tunes"
 
  <?php foreach ($tunes as $tune) { echo $tune->title."<br>";} ?> // To test this strategy
</div> 


I've tried several variations but always get
error: Severity: Notice
Message: Undefined variable: tune
Filename: partials/topper_ssv.php
Line Number: 3

Any idea what dumb thing I'm doing wrong?
Reply
#13

(09-26-2017, 03:54 PM)codeguy Wrote: 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.

Sorry I missed that post of yours. Any data is passed to the very first view. Views themselves still check to see if the data exists and if not deals with is smoothly, so there is no chance of errors showing if data is NULL or missing.

I recall that I used to pass data to all the views separately under the mistaken belief that the data passed to a view was removed once the view was processed. When I discovered this was not the case I carried on for a while passing view data manually to each view. After a while though it dawned on me what a waste of time this was. Now I simply load all the data into one variable and pass it to the very first view. I probably should load it into the view vars as described earlier by someone, but I have never had a problem passing it all the the header view, and having it all available to all other views.

My first view is always the header_view with the HTML, additional css, meta tags etc. The next is usually the main_menu_view with navigation and active states on the current page options etc. My page layouts I do in my own template library and set the page_data variables in my controller with library setters etc.

In fact, I now quite like that the view data is not deleted, and am hoping this will not change in CI4.

This approach makes changing the site template or design very easy to do, as I am only dealing with HTML layouts and css, never logic or data processing of any sort.

Hope that helps,

Paul.

PS I am very careful though to only pass the data that is required. So I always try to define the fields in my models using select statements rather than the quicker but dirtier method of relying on the fact that the query builder, if not told, will select all by default. Those are called by libraries that format the dates, currencies, etc. Finally any fields that need cleaning are done in the libraries, so the view files are as clean of php as possible, except of course for loops and basic if statements.
Reply
#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
#15

(09-28-2017, 10:50 AM)PaulD Wrote: 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:
@PaulD Thanks much for this. As soon as I click SEND on this I'm walking to my favorite coffee shop with my tablet and notepad to absorb it and reread your previous replies - to digest them to the point where I can redo my code using these ideas. I'll let you know soon how it goes.
Reply
#16

@PaulD Did a lot of reading yesterday. Can't understand why something as conceptually as simple as a template for a view can be so obscure in the CI context. Anyway, this AM I tried to just copy/past code from your posts, adapted to my environment. My home controller looks like this now:


PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
C_home extends CI_Controller {
    public function __construct()   {
        parent::__construct();
        $this->load->model('M_tunes');
        $this->load->model('M_filters');
        $this->load->model('M_links');
        $this->load->helper('url');
    }
    public function index() {
        // basic layout data - just example of setting some data
        $this->template_library->set_page_data('meta_title''mz_Library');
        $this->template_library->set_page_data('page_title''MZ Library');
        // dynamic data collection
        $this->template_library->set_page_data('template'$this->load->view('partials/topper_ssv'));
        // render view
        $this->template_library->render_page('home_view');
    }

I've tried several variations of method names / capitalization, etc. but keep getting errro on 1st line in index():

PHP Code:
$this->template_library->set_page_data('page_title''MZ Library'); 

Error Message: "Undefined property: C_home::$template_library"

The template_library file in aopplication/libraries is:
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();
    }
    // setter to add data to page_data from controller
    public function set_page_data($name$data NULL) {
        $this->page_data[$name] = $data;
        return;
    }
    // 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_v'$this->page_data);
        $this->CI->load->view($template);
        $this->CI->load->view('common/footer_v');
    }


Any suggestions?
Reply
#17

Have you loaded template_library?
Reply
#18

(09-30-2017, 12:57 PM)dave friend Wrote: Have you loaded template_library?

Right. Stands to reason that after creating new files and editing some existing ones to do this new thing - use a template in CI - I also need to load the new template _library file that does the magic. Thank you. Fixed it. It seems to be attempting to load the view now but there's something about a "tune" variable not being defined - which coincidentally was the same problem I had before I started this plea for help. But now I think I might be able to figure it out from here - thanks to the great advice from you guys. Fingers crossed.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB