Reputation: 509
I'm creating a website with CodeIgniter & have a question on best practice.
I generally break my webpages up into modular chunks. Header, Content, Footer (and some other stuff sprinkled in between as needed.
The 'Header' and 'Footer' chunks are usually static, while the guts in between can change depending on a few variables (the actual page for example - home, about, contact etc.)
Below is the controller for the page. header_view, navigation_view, and footer_view will (most likely) never change. The home_main_view will.
public function index()
{
$this->load->view('header_view');
$this->load->view('navigation_view');
$this->load->view('home_main_view');
$this->load->view('footer_view');
}
If a user navigates to the about page for example, the views may look something like this:
public function index()
{
$this->load->view('header_view');
$this->load->view('navigation_view');
$this->load->view('about_view');
$this->load->view('footer_view');
}
How would I handle that change in CI? Is it best to make home_main_view a variable and pass is into the index() function? I've done this before (user clicks a link that triggers a function in the controller that sets $var that calls index($var) that calls view($var).
Upvotes: 1
Views: 3543
Reputation: 7388
Please see my answer in the SO thread entitled Header and footer in CodeIgniter. This is how I handle all my views. It's a fairly modular approach - hope it helps!
As an example, a homepage might be loaded like this:
class Home extends CI_Controller{
function index(){
$d['v'] = 'home';
//do database interaction here and
//ultimately assign results to some key in $d too.
$this->load->view('init', $d);
}
}
Upvotes: 0
Reputation: 1301
My favorite approach is to use a layout (it's just a view) that I use to load the other components in place. You obviously need one layout for each type of page you have, but using layout makes later changes easy.
I went one step further and created a method in my custom controller MY_Controller
that I use to load the layout:
<?php
class MY_Controller extends CI_Controller{
/**
* Global $data variable holder
* @var stdClass
*/
protected $data;
/**
* The templates path, used by the 'render' method to determine the template to load
* This is the 'root' of the templates folder for a specific section
* Must NOT contain a trailing slash!
* @var string
*/
protected $templates_path = 'site';
/**
* The layout used by the 'render' method to load the specified view
* @var string
*/
protected $layout = 'default';
/**
* Class initialisation
*/
function __construct() {
parent::__construct();
/*init the $data variable*/
$this->data = new stdClass();
/*the base url shortcut - to be used where needed*/
$this->data->base_url = $this->config->item('base_url');
/*page encoding header*/
$this->output->set_header('Content-Type: text/html; charset=utf-8');
}
/**
* Helper method that will load a view using the layout configured
* @param type $view
*/
protected function render($view = FALSE){
$this->data->page_template = $this->templates_path.'/'.$view;
$this->data->templates_path = $this->templates_path;
$this->load->view($this->templates_path.'/layouts/'.$this->layout, $this->data);
}
}
Then, in my layout, I load the view indicated in the $page_template
variable in the proper place
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
<!DOCTYPE html>
<html>
<head>
<?$this->load->view($templates_path.'/sections/head')?>
</head>
<body class="<?=$page_class?>">
<?$this->load->view($templates_path.'/sections/header')?>
<?if(isset($hpslider) && $hpslider)
$this->load->view($templates_path.'/sections/hp_slider')?>
<div class="pcontent">
<div class="page">
<?=$this->load->view($page_template);?>
</div>
</div>
<?$this->load->view($templates_path.'/sections/footer')?>
</body>
</html>
Hope this helps you out.
Upvotes: 2
Reputation: 558
I don't use CI but u can use $_post , $_get or segments to let the action know what page you want to load.
Segment example: when u goto example.com/index.php/controller/index/home then it will load the "home" view.
public function index(){
$page=$this->uri->segment(3);
$this->load->view('header_view');
$this->load->view('navigation_view');
$this->load->view($page);
$this->load->view('footer_view');
}
http://codeigniter.com/user_guide/libraries/uri.html
Upvotes: 1