• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to handle controller for every reload ?

#1
[eluser]hykoh[/eluser]
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 ?

#2
[eluser]Yash[/eluser]
Qs is not so clear to me..but constructor can do the trick

#3
[eluser]Dready[/eluser]
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 :

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

and an helper like :

Code:
function render_output($main_html) {
  $ci=&get;_instance();
  $tpl = array ('main'=>$main_html);
  $tpl['left_menu'] = $ci->menu->get_html();
  $tpl['other_part'] = other_thing();
  $ci->parser->parse('body',$tpl);
}

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.

#4
[eluser]hykoh[/eluser]
it looks good Wink i'll try it at home...

thanks

#5
[eluser]SitesByJoe[/eluser]
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):

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

My view:

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

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

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.

#6
[eluser]meigwilym[/eluser]
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

Code:
// 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);


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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