As you can see from our MY_Controller, we created some render()
method that will create the views. Also, the default template is named “public_master_view” and is called from the application/views/templates
directory. So let’s create one.
We will save a public_master_view.php file inside views/templates directory (which we will create…). The public master view will simply call a public header view and a public footer view from templates/parts directory:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); $this->load->view('templates/_parts/public_master_header_view'); ?> <?php echo $the_view_content;?> <?php $this->load->view('templates/_parts/public_master_footer_view');?
Now we will create the public_master_header_view.php
inside application/views/templates/_parts
directory:
<?php defined('BASEPATH') OR exit('No direct script access allowed');?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title><?php echo $page_title;?></title> <meta name="description" value="<?php echo $page_description;?>" /> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <?php echo $before_closing_head;?> </head> <body>
and the public_master_footer_view.php
inside the same application/views/templates/_parts
directory:
<?php defined('BASEPATH') OR exit('No direct script access allowed');?> <footer> <div class="container"> <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p> </div> </footer> <?php echo $before_closing_body;?> </body> </html>
We also need to create the template views for the authenticated users. So simply duplicate auth_master_view.php
and replace the names of the header and footer views with
auth_master_header_view.php
and respectively auth_master_footer_view.php
.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); $this->load->view('templates/_parts/auth_master_header_view'); ?> <?php echo $the_view_content;?> <?php $this->load->view('templates/_parts/auth_master_footer_view');?>
Also, duplicate the template header view and template footer view in templates/_parts changing the names accordingly to auth_master_header_view.php
and auth_master_footer_view.php
.
Now let’s see if it all works. Inside the User.php we created a login() method. Let’s change it so that it will use the render()
method we’ve defined in the MY_Controller
.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class User extends MY_Controller { function __construct() { parent::__construct(); $this->load->library('ion_auth'); } public function index() { $this->load->view('welcome_message'); } public function login() { $this->data['message'] = 'here will be the login form'; $this->render('user/login_view'); } public function logout() { echo 'here we will do the logout'; } }