Create Template in CodeIgniter Framework


In CodeIgniter Project, Open autoload.php file in config folder. Add url helper to helper config as below:

$autoload['helper'] = array('url');

In CodeIgniter Project, Open config.php file in config folder. Set value for base_url config as below:

$config['base_url'] = 'http://localhost:9092/LearnCodeIgniterWithRealApps/';




Create new folder named templates in views folder. In templates folder, create new PHP file named mytemplate.php as below:

<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<html>
    <head>
    	<title><?php echo $title; ?></title>
    </head>
    <body>

    	<a href="<?php echo site_url('home'); ?>">Home</a> |
    	<a href="<?php echo site_url('aboutus'); ?>">About Us</a> |
    	<a href="<?php echo site_url('news'); ?>">News</a>
    	<br><br>
    	<?php $this->load->view($page); ?>
    	<br><br>
    	Copyright PMK Lab

    </body>
</html>

In controllers folder create new controllers as below:

Create new PHP file named home.php in controllers folder as below:

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

class Home extends CI_Controller
{

    public function index()
    {
        $data['title'] = 'Home Page';
        $data['page'] = 'home/index';
        $this->load->view('templates/mytemplate', $data);
    }

}

Create new PHP file named aboutus.php in controllers folder as below:

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

class AboutUs extends CI_Controller
{

    public function index()
    {
        $data['title'] = 'About Us Page';
        $data['page'] = 'aboutus/index';
        $this->load->view('templates/mytemplate', $data);
    }

}

Create new PHP file named news.php in controllers folder as below:

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

class News extends CI_Controller
{

    public function index()
    {
        $data['title'] = 'News Page';
        $data['page'] = 'news/index';
        $this->load->view('templates/mytemplate', $data);
    }

}




In CodeIgniter Project, Open routes.php file in config folder. Set value for default_controller as below:

$route['default_controller'] = 'home';

In views folder, create new views as below:

Create new folder named home. In home folder, create new view as below:

<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<h3>Home Page</h3>

Create new folder named aboutus. In aboutus folder, create new view as below:

<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<h3>About Us Page</h3>

Create new folder named news. In news folder, create new view as below:

<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<h3>News Page</h3>




Access index action in home controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/home/index

Output

Access index action in aboutus controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/aboutus/index

Output

Access index action in news controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/news/index

Output

I recommend you refer to the books below to learn more about the knowledge in this article: