m4st3r Rulz
m4st3r Rulz

Reputation: 9

am I doing right with this codeigniter templating?

I want to create a template codeigniter , but i dont know if I am doing right , this is what I got so far.. how someone tell if I am doing right? i am using codeigniter 3 , and bootstrap 4 ... the web app should be also using websocket for dashboard .

template

  <div class="menu-wrap">
    <?php $this->load->view('landing/menu-wrap'); ?>
</div>
<!-- Header Section Start -->
<header id="video-area" data-stellar-background-ratio="0.5">
    <div id="block" data-vide-bg="video/video"></div>
    <?php $this->load->view('landing/video-area'); ?>
</header>
<!-- Features Section End -->
<section id="services" class="section">
    <?php $this->load->view('landing/services'); ?>
</section>
<!-- Services Section Start -->

<!-- Services Section End -->

<!-- Features Section Start -->
<section id="features" class="section" data-stellar-background-ratio="0.2">
    <?php $this->load->view('landing/features'); ?>
</section>
<!-- Start Video promo Section -->
<section class="video-promo section" data-stellar-background-ratio="0.5">
    <div class="overlay"></div>
    <?php $this->load->view('landing/video-promo'); ?>
</section>
<!-- End Video Promo Section -->

<!-- Portfolio Section -->
<section id="portfolios" class="section">
    <!-- Container Starts -->
    <?php $this->load->view('landing/portfolios'); ?>
        <!-- Container Ends -->
</section>
<!-- Portfolio Section Ends -->

<!-- Start Pricing Table Section -->
<div id="pricing" class="section pricing-section">
    <?php $this->load->view('landing/pricing'); ?>
</div>
<!-- End Pricing Table Section -->

<!-- Counter Section Start -->
<div class="counters section" data-stellar-background-ratio="0.5">
    <div class="overlay"></div>
    <?php $this->load->view('landing/counters'); ?>
</div>
<!-- Counter Section End -->

<!-- testimonial Section Start -->
<div id="testimonial" class="section">
    <?php $this->load->view('landing/testimonial'); ?>
</div>
<!-- testimonial Section Start -->

<!-- Download Section Start -->
<section id="download" class="section">
    <?php $this->load->view('landing/download'); ?>
</section>
<!-- Download Section End -->

<!-- Blog Section -->
<section id="blog" class="section">
    <!-- Container Starts -->
    <?php $this->load->view('landing/blog'); ?>
</section>
<!-- blog Section End -->

<section id="contact" class="section">
    <?php $this->load->view('landing/contact'); ?>
</section>
<!-- Contact Section End -->

<!-- Subcribe Section Start -->
<div id="subscribe" class="section">
    <?php $this->load->view('landing/subscribe'); ?>
</div>
<!-- Subcribe Section End -->

Upvotes: 0

Views: 101

Answers (1)

Himanshu Upadhyay
Himanshu Upadhyay

Reputation: 6565

The ideal way to use template in codeigniter is like this:

make one view file in your views folder say template.php

<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title><?php echo site_name(); ?></title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        ....
        //Load your css files here
        ....
    </head>
    <body>
    // All common HTML content like header and all parent div(s) with class like container (is using bootstrap) will go here.
        <!-- Wrapper Start -->
        <div class="wrapper">
            <div class="structure-row">
            <!-- Header -->
            <?php echo $this->load->view('blocks/header');  ?>
            <?php echo $content; ?>
            </div>
        </div>
        <!-- Footer -->
        <?php echo $this->load->view('blocks/footer'); ?>
    </body>
</html>

Then create one library file in libraries folder Template.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Template
 *
 * Enables the user to load template
 *
 * Usage:
 *
 * Load it within your Controllers:
 * $this->load->library("Template");
 *
 * Configure CodeIgniter to Auto-Load it:
 *
 * Edit application/config/autoload.php
 * $autoload['libraries'] = array('Template');
 *
 *
 * Use it in your view files
 * $this->template->view('view', $parameters);
 * 
 */
class Template {

    var $obj;
    var $template;

    public function __construct($template = array('template' => 'template'))
    {
        $this->obj =& get_instance();
        $this->template = $template['template'];
    }

    /**
     *
     * set_template()
     *
     * Sets the template 
     * 
     */
    public function set_template($template)
    { 
        $this->template = $template;
    }

    /**
     *
     * view()
     *
     * Loads the view 
     * 
     */
    public function view($view, $data = NULL, $return = FALSE)
    { 
        $CI = & get_instance();

        $loaded_data = array();
        $loaded_data['content'] = $this->obj->load->view($view, $data, true);



        if ($return)
        {
            $output = $this->obj->load->view($this->template, $loaded_data, true);
            return $output;
        }
        else
        {
            $this->obj->load->view($this->template, $loaded_data, false);
        }
    }

}

Then in your config/autoload.php file load the above given library like this:

$autoload['libraries'] = array('database', 'session', 'template');

Now finally you can load your own views within the template like this:

$this->template->view('folder_name/view_file_name');

Hope this will be helpful to you. If you need more info, you can write it in comment.

Upvotes: 3

Related Questions