Wednesday, July 14, 2010

Codeigniter Advanced Layout Library

We have several layout library in the wiki of codeigniter. But those has only one replacement block. Advanced codeigniter library solves this problem. You can find the features of advanced layout library here here. Let's discuss about Advanced Layout library for codeigniter.

we will see every code block of the library and discuss them seperately. At first we have defined our necessary variables.

#ci instance
    public $ci;
    #layout variable
    public $layout;

In the constructor function we have created an instance of codeigniter. the argument of the function gets the filename of our main codeigniter library.

/**
     * Constructor
     *
     * @author Tahsin Hasan 
     * @return void
     * @param  layout String layout file name
     * @access public
     */
    public function __construct($layout = "layout_main") {
        $this->ci =& get_instance();
        $this->layout = $layout;        
    }
Let's see the code block of the function that handles the block replacement of our layout. we are passing here three arguments: blocks array contains key-value relationship of block and their corresponding replacement code chunk. view is the filename of the main body content area.
/**
     * shows our blocks in the layout section
     *
     * @author Tahsin Hasan 
     * @return void
     * @param  blocks Array blocks to replace
     * @param  view String code to replace with
     * @param  data Array the data we need to pass
     * @access public
     */
    public function view($blocks, $view, $data)
    {   
        $layout       = $this->layout;
        $data['content_for_layout'] = $this->ci->load->view($view, $data, true);
        
        $view = $this->ci->load->view($layout, $data, true);
        foreach($blocks as $key=>$val)
        {
           if(empty($val))
           {
               $replaces[$key] = '';
           }
           else
           {
               $replaces[$key] = $this->ci->load->view ( $val, NULL, TRUE );
           }            
        }
        $view = str_replace(array_keys($replaces), array_values($replaces), $view);
        
        $this->ci->load->view ( 'view', array ('view' => $view) );
    }

The content_for_layout variable contains the main body section of a page. We replace this string value with our page specific content. with this page specific content, we then replace the layout with our values and store it in the view file.

Download Advanced Layout library from here.
The block variable contains the other replacements except the main content. we replace each varible with the foreach loop.

the str_replace function done the tricks here. it replaces the keys with the values of the view variable. And finally, we load the view file.

Download Advanced Layout library from here.


See the book OpenCart 1.4 Template Design Cookbook.
See the book Joomla Mobile Development Beginners Guide




List of my works:

Opencart Extensions:

  1. Product Based Quantity Wise Shipping: Find it here.
  2. OpenSSLCOMMERZ: integrate SSLCOMMERZ with opencart: Find it here.
  3. Fine Search v.1.0 - Improves Opencart search feature to find relevant: Find it here.
  4. Opensweetcaptcha - An easy way to generate attractive captcha for your system!: Find it here.
  5. Custom Field Product - add unlimited custom fields to the product form: Find it here.
  6. Formcaptcha - add captcha on the register page: Find it here.

My Books:

  1. OpenCart 1.4 Template Design Cookbook.
  2. Joomla Mobile Development Beginners Guide

No comments: