Skip to content

Page Templates

Paul Titus Obligado edited this page Jul 10, 2014 · 2 revisions

The page template is used when one wants to make multiple pages to have the same layout or design. For example, eValuation uses page templates to provide layout for a page.

An example of a template is this default layout.

Page Template

The template's view file looks like this:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{page_title}</title>
    <?php $this->load->view('partials/head');?>
  </head>
  <body>
    <div id="main-wrapper" class="default container-fluid">
      <div id="main-header" class="row"><?php $this->load->view('partials/header');?></div>
      <div id="main-content" class="row">{body_content}</div>
      <div id="main-footer" class="row"><?php $this->load->view('partials/footer');?></div>
    </div>
  </body>
</html>

This template automatically generates the header, footer, and head as specified in the partials folder.

For this template, one only needs to provide the {page_title} and {body_content} as will be specified in the controller.

View

The view page to be loaded or inserted into the page template should only contain code that you would load inside the <body> tag as it will be inserted inside the main-content div container. For example:

<?php echo form_open('session/login');?>

  <div class="control-group">
    <div class="controls">
      <input id="username" name = "username" type = "text" value="<?php echo $this->session->flashdata('username'); ?>" placeholder = "Username" class="field">
      <?php echo $this->session->flashdata('username_error');?>
    </div>
  </div>

  <div class="control-group">
    <div class="controls">
      <input id="password" name = "password" type = "password" value="<?php echo $this->session->flashdata('password'); ?>" placeholder = "Password" class="field">
      <?php echo $this->session->flashdata('password_error');?>
      <?php echo form_error('verify_login','<p class="text-error">','</p>'); ?>
    </div>
  </div>

  <div class="control-group" id="login-button-container">
    <div class="controls">
      <input type="submit" value="Log in" class = "button-primary">
    </div>
  </div>
</form>

Controller

The controller is responsible for presenting the page template to the browser. An example of a controller using the page template is this:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {
  public function index() {
    $data['page_title'] = "eValuation";
    $data['body_content'] = $this->load->view('contents/home','',TRUE);
    $this->parser->parse('layouts/default', $data);
  }
}

To use the page template, create an array with page_title and body_content indices. Then store the title and the content to the array.

The body_content index should contain the data of the view you wish to display. You can do this using the load view function of CodeIgniter with a boolean TRUE as the third parameter.

$data['body_content'] = $this->load->view('contents/home','',TRUE);

You can then parse the page template using the array as the second parameter.

$this->parser->parse('layouts/default', $data);

Clone this wiki locally