Welcome Guest, Not a member yet? Register   Sign In
CI 3 should has partial view or user control
#4

Here is a very code to show the concept.
Code:
class Template {
  private $CI;

  public function __construct()
  {
    $this->CI =& get_instance();
  }

  public function generate($content)
  {
    $header = $this->CI->load->view('header', null, TRUE);
    $footer = $this->CI->load->view('footer', null, TRUE);
    $this->CI->output->set_output($header . $content . $footer); //use output so CI can cache results if caching enabled
  }
}

Code:
$autoload['libraries'] = array('template');

Then in a controller...

Code:
function something()
{
  //...do everything needed to for this page's content
  $data = array();  //data for this contents view
  $content = $this->load->view('content_for_this_page', $data, TRUE); //return view instead of directly outputting it

  //now just pass this pages view to the template, which will automatically generate header/footer
  $this->template->generate($content);
}
Reply


Messages In This Thread
RE: CI 3 should has partial view or user control - by CroNiX - 02-09-2015, 11:05 AM



Theme © iAndrew 2016 - Forum software by © MyBB