Welcome Guest, Not a member yet? Register   Sign In
How to handle controller for every reload ?

How can I handle outer controller ? e.g. in every site, $this->load->view('header'); and $this->load->view('footer'); get included.

I post already another thread, nearly with the same thematic ... how can I load EVERYTIME the script runs through, definied functions like a language include for the menu inside the "header" ? Or if the menu must get dynamically out of a database ? What is the best way to include some functions/controller/models at every page load ?

Qs is not so clear to me..but constructor can do the trick

That's a good question. You'll perhaps find utilities that do this for you (advanced templates classes, etc) in the wiki. My personnal trick is to create an helper function that I call at the end of any controller function, and that adds all the stuff. Something like :

// inside controller function
... processing ...
$main_html = $this->parser->parse('whatever',$tplarray,true);
return render_output($main_html);

and an helper like :

function render_output($main_html) {
  $tpl = array ('main'=>$main_html);
  $tpl['left_menu'] = $ci->menu->get_html();
  $tpl['other_part'] = other_thing();

That's enough, and you have the choice not to call your helper function, for example in case of a controller method returning an AJAX response in json, or whatever.

it looks good Wink i'll try it at home...


Here's the bit you are missing - your view can call other views. I usually pass the content bits within each page as an array from the controller, sort of like this:

My Controller (snippet):

// I'm skipping the other controller code
$data['contents'] = array(
// load your main view from the controller and pass over your array
$this->load->view('my_view', $data);

My view:

    <?php $this->load->view('header'); ?>
    // loop through your array and add your views
    foreach($contents as $section)

    <?php $this->load->view('footer'); ?>

This is a very simple example at least to get the idea across. I recall having trouble absorbing this when I first started with CI.

Here's a quick example using a simple app that I'm currently building.

I've got two global views header.php and main.php.

Each file has <?php echo $content; ?>.

header.php is the html for the <head> and main.php has everything else.

I then have more views, with 2 or 4 columns, which I can use as I want.

In the controller I have

// load some JS first...
$head['content'] = $this->load->view('jquery', '', TRUE);

// then get the html...
$html = $this->yourmodel->get();

$data['content'] = $this->load->view('two-col', $html, TRUE);

$this->load->view('_global/header', $head);
$this->load->view('_global/main', $data);

Theme © iAndrew 2016 - Forum software by © MyBB