CodeIgniter Forums

Full Version: loading a view from a variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Nir Gavish[/eluser]
i need to pre-process a view before it is loaded - that is: get it from file, manipulate it, and then load it as a view. while the $return param allows me to *post* process the view, there appears to be no mechanism in-place to *pre*process it. did anyone else encounter this need? what is the most elegant solution? thanks in advance.

El Forum

[eluser]Michael Wales[/eluser]
Code:
$myview = $this->load->view('myview', $data, TRUE);
// do your processing here
$this->output->set_output($myview);

El Forum

[eluser]Nir Gavish[/eluser]
thanks for a fast reply. however, this is not what i'm looking for. i'll elaborate:

i have a view which is the skin of the website, header, footer, sidebar, with an area for content.

now, normally, i would have a 'load->view' method in the content area, which will load the correct view.

however, what i'm aiming for is that this view will contain only "<div id='content'></div>", and i will process it into "<div id='content'>&lt;?$this->load->view($x)?&gt;</div>"

i cannot do it with $return=true, since the view will already be processed, and thus i cannot load internal views.

is that making any sense?

El Forum

[eluser]Michael Wales[/eluser]
Ah - this is what I call partial views and I discuss it in this blog article, although that article goes well beyond what you are looking for. Here's a quick rundown:

layout.php
Code:
&lt;?php
  $this->load->view('header');
  $this->load->view($partial);
  $this->load->view('footer');

controller
Code:
function index() {
  $data['partial'] = 'users/index';
  $this->load->view('layout');
}

Your variables within the $data array will be passed on from layout.php to all of it's child views automagically.

El Forum

[eluser]Nir Gavish[/eluser]
hi, thanks again, i understand the concept of partial views, but this is one step further than your avarage partial view, the chief-view isn't meant to contain any php instructions whatsoever, i wish to inject the php *prior* to the execution of the view. this is done so that the chief-view (or master-page / template / whatever you may call it) contains only simple html, and not one line of php.

El Forum

[eluser]Nir Gavish[/eluser]
hi again, after some inspection, it appears i will have to extend the CI loader if i want this done, add a method like load_view_from_string() or something along those lines, does anyone have any experience with that?