Fork me on GitHub

Codeigniter Simplicity

The simplest Codeigniter Template Library you've ever saw!

Codeigniter Simplicity is a Codeigniter Template Library that is here to make our life easier.

It's been a while that there is not any official Template/Layout library inside Codeigniter. In that case I've created the Codeigniter Simplicity that is based on core functionalities of Codeigniter as it represents to me the "How it should be" project.

The code is really simple to use and the naming of the functions are pretty obvious of what they are doing without any more complexity

Obviously the first thing that you have to do is downloading the library. Make sure that you have Codeigniter at your project before using this library. They are two options to download the library:

Download latest stable version 0.7 or download latest development version from github

Once downloaded, unzip the compressed folder and copy the folder into your codeigniter Project. That's it really! You don't need any configuration to do and your previous project will be as it was before.

To help you understand how your project will be after the copying of the files and folder, it should look something like this:

application
├──cache
├──config
├──controllers
│  ├──welcome.php
│  ├──index.html
│  ├──example.php
├──core
│  ├──index.html
│  ├──MY_Loader.php
│  ├──MY_Output.php
├──errors
├── ...
├──views
├────themes
│     ├──simple.php
│     ├──blank.php
│     ├──default.php
│     ├── ...
│  ├── ...
assets
├──themes
├────default
├──────css
│        ├──general.css
│        ├──bootstrap.css
│        ├── ...
├──────js
│        ├──jquery-1.9.1.min.js
│        ├──bootstrap.min.js
│        ├── ...
├──────images
│        ├──glyphicons-halflings.png
│        ├──favicon.png
│        ├── ...
system
index.php

This is a good structure for your templates

A good way to start actually is by checking the examples that the project has inside. There is a Controller with name Example and there they are lot of examples of how to use Codeigniter Simplicity

The Controller Example will look something like this:
<?php
 
class Example extends CI_Controller {
 
    function __construct()
    {
        parent::__construct();
 
        $this->load->helper('url');
 
        $this->output->set_template('default');
    }
 
    public function index()
    {
        $this->load->view('ci_simplicity/welcome');
    }
 
    public function example_1()
    {
        $this->load->view('ci_simplicity/example_1');
    }
 
    public function example_2()
    {
        $this->output->set_template('simple');
        $this->load->view('ci_simplicity/example_2');
    }
 
    public function example_3()
    {
        $this->load->section('sidebar', 'ci_simplicity/sidebar');
        $this->load->view('ci_simplicity/example_3');
    }
 
    public function example_4()
    {
        $this->output->unset_template();
        $this->load->view('ci_simplicity/example_4');
    }
}
 
And the template file:
 
<html lang="en">
    <head>
        <title><?php echo $title; ?></title>
        <meta name="resource-type" content="document" />
        <meta name="robots" content="all, index, follow"/>
        <meta name="googlebot" content="all, index, follow" />
    <?php
    /** -- Copy from here -- */
    if(!empty($meta))
    foreach($meta as $name=>$content){
        echo "\n\t\t";
        ?><meta name="<?php echo $name; ?>" content="<?php echo $content; ?>" /><?php
             }
    echo "\n";
 
    if(!empty($canonical))
    {
        echo "\n\t\t";
        ?><link rel="canonical" href="<?php echo $canonical?>" /><?php
 
    }
    echo "\n\t";
 
    foreach($css as $file){
         echo "\n\t\t";
        ?><link rel="stylesheet" href="<?php echo $file; ?>" type="text/css" /><?php
    } echo "\n\t";
 
    foreach($js as $file){
            echo "\n\t\t";
            ?><script src="<?php echo $file; ?>"></script><?php
    } echo "\n\t";
 
    /** -- to here -- */
?>
        <!--[if lt IE 9]>
            <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
    </head>
<body>
<?php echo $output; ?>
</body>
</html>
 
Code Description Example
$this->output->set_template($template) Set a template $this->output->set_template('default');
$this->output->unset_template() Unset any template that was set $this->output->unset_template();
$this->load->section($section, $ci_view [, $data ]); Load a section to the template $this->load->section('sidebar', 'ci_simplicity/sidebar');
$this->load->get_section($section); Get the section at the template $this->load->get_section('sidebar');
$this->load->js($js_files); Load js files $this->load->js('assets/js/jquery.min.js');
$this->load->css($css_files); Load css files $this->load->css('assets/css/general.css');
$this->output->set_canonical($canonical); Add canolical metatag $this->output->set_canonical(site_url('about'));
$this->output->set_meta($name,$content); Add metatag $this->output->set_meta('description','Lorem Ipsum');
$this->output->set_common_meta($title, $description, $keywords) Set common meta Data $this->output->set_common_meta('About me', 'This is a page about me', 'about, John, CV');
Note: The below example is an iframe so it might appeared with a scroll bar or in some cases it may not appear at all! If you are experiencing problems with the iframe you can also see the example at a new tab
v0.7
	#5 - Fix error Array to string conversion by implode meta keywords if its value is array
	Compatibility with Codeigniter 3.x
v.0.6
	Load CSS files
	Load JS files
	Set Template
	Set Sections
	Set Meta Data
	Set Title
	Set Common Meta Data
	Set Canonical Meta Data
	Load CSS and JS files inside the view
	Unset Template

If you've successfully installed the template library, the very next step would be to create a CRUD for your Codeigniter application. Fortunately this is very easy as well and you can achieve a full CRUD with just few lines of code.

Grocery CRUD is a completely free library for Codeigniter and It is seriously very easy to work with. For example in order to have a full CRUD, you will just need the below lines of code:

$crud = new grocery_CRUD();
$crud->set_table('customers');
$crud->set_subject('Customer');
 
$output = $crud->render();

For more about Grocery CRUD, you can visit the page: www.grocerycrud.com