Welcome Guest, Not a member yet? Register   Sign In
New Layout View Library Supporting Partial Views
#1

[eluser]Gyorgy Fekete[/eluser]
Update 2 available here: http://ellislab.com/forums/viewthread/62521/

Hello,
Let's face it CodeIgniter is good, but sucks on partial views. So I created a layout based view on top of the existing view method that CI offers.

How it works:
I view can be a partial view (a part of the page) or a layout view which defines where the partial views are on the page. For ex:

layout view
Code:
<?=$header;?>
<?=$content;?>
<?=$footer;?>

header view
Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
...

Now the tricky part comes in. In theory a partial view could be also a layout view. This is no problem for the library. You can set a new layout inside a master layout and add partials to that layout or create a new layout inside of it and so on...

As you can see I suck on writing tutorials. So I'm just going to paste here the library and attach an example files.

Code:
<?php
class Layout
{
    var $obj;
    var $_layout;
    var $_parts = array();
    var $_datas = array();
    
    /**
    * @param array If a config file exists CI autoloads it then we have to parse it.
    */
    function __construct($configData = null)
    {
        $this->obj =& get_instance();
        
        if ($configData)
        {
            $this->readConfigData($configData); //reading the config data
        }
        
    }
    
    function readConfigData($data)
    {
        $parts = $data['parts'];
        $this->_layout = $data['layout'];
        
        foreach ($parts as $key => $value)
        {
            if ($value == ':layout:')
            {    
                $this->_parts[$key] = new Layout($data[$key]);
                $this->$key =& $this->_parts[$key];
            }
            else
            {
                $this->_parts[$key] = $value;
            }            
        }
    }
    
    /**
    * Passing a data array to a specific part view
    *
    * @param string The name of the partial view (not the file name)
    * @param array The data to be passed to this partial view
    */
    function addData($part,$data = array())
    {
        if ( $this->hasPart($part) )
        {
            if ( $this->hasData($part) )
            {
                $this->_datas[$part] = array_merge($this->_datas[$part],$data);
            }
            else
            {
                $this->_datas[$part] = $data;
            }
        }
    }
    
    /**
    * Adds a partial view to the layout
    *
    * @param string The name of the partial view (this will be passed as a data array to the master layout)
    * @param string The filename of the partial view (with or without a subfolder)
    * @param array Optional data array to be passed to the partial view
    */
    
    function part($part,$view,$data = array())
    {
        $this->_parts[$part] = $view;
        $this->_datas[$part] = $data;
    }
    
    /**
    * Creates a new layout
    *
    * @param string The name of the new layout
    */
    
    function newlayout($part)
    {
        $this->_parts[$part] = new Layoutext();
        $this->$part =& $this->_parts[$part];
    }
    
    /**
    * Check is a partial view exists
    *
    * @param string The name of the partial view
    */
    
    function hasPart($part)
    {
        return isset($this->_parts[$part]);    
    }
    
    /**
    * Check if a partial view has data
    *
    * @param string The name of the partial view
    */
    
    function hasData($part)
    {
        return isset($this->_datas[$part]);
    }
    
    /**
    * Sets the layout
    *
    * @param string The filename of the layout
    */
    
    function layout($srcLayout)
    {
        $this->_layout = $srcLayout;
    }
    
    /**
    * Gets the layout filename
    */
    
    function getLayout()
    {
        return $this->_layout;
    }
    
    /**
    * Outputs the whole layout
    *
    * @param bool Whether to output it to the bowser or return it for reuse.
    */
    
    function view($return = false)
    {
        $tmp_parts = array();
        $tmp_data = array();
        foreach ($this->_parts as $part => $view)
        {
            if (isset($this->_datas[$part]))
            {
                $tmp_data = $this->_datas[$part];
            }
            
            if (is_object($view) )
            {
                $tmp_parts[$part] = $view->view(true);
            }
            else
            {
                $tmp_parts[$part] = $this->obj->load->view($view,$tmp_data,true);
            }
        }
        
        $output = $this->obj->load->view($this->_layout,$tmp_parts,$return);
        
        if ($return) {
            return $output;
        }
    }
}
?>

The attached example contains test views, layouts and a test controller and also an autoloading config file. Just copy these files to you CodeIgniter project and load the test controller like this: http://ci_project/test/config_yes or http://ci_project/test/config_no

Actually this is the link to the example file: http://www.primalskill.com/

- ci_project is your CodeIgniter project.
- config_yes loads the layout library with the autoload config
- config_no loads the layout library and builds up the test_layout you have to delete the autoloading config file first!

I hope this library will be beneficial to all CodeIgniter users. If you have any question you can send me a PM or reply here. I'm happy to answer all of your questions.
#2

[eluser]sophistry[/eluser]
Hi Gyorgy,

It's nice to see a first posting be a complete solution to a niggling problem!

Would you do us a favor and describe any differences you see between your approach and the View Library by coolfactor. They seem to be quite similar.

Also, please document your findings at the FAQ or just put a link to this thread if you think it worthwhile!

Best!
#3

[eluser]Gyorgy Fekete[/eluser]
Yes, indeed the two code are similar, but when I wrote mine I didn't had knowledge about coolfactor's View library. I'll have a look into that code I write down the similarities and differences between the two.
#4

[eluser]Gyorgy Fekete[/eluser]
Okay, the differences between the 2 libraries are:

My library is just version one, therefore doesn't offer as much features as coolfactor's view library. For ex. mine doesn't supports cacheing, yet. but that's relative because it uses the view method of the framework, it doesn't put itself on top of it.

My library uses the codeigniter's native config methods. Loading and parsing configuration files. It can load up several partial views, layout files, etc. (Update 2 will also support auto loading data into these partial views)

I only support loading data into views as an array, and not as key, value pairs. It will be changed in update 2

Also my library supports partial layouts too. This means that a partial view can be defined as a partial layout and this can have also partial views and so on.

I think that' about it. I will work on update 2 as soon as possible. I hope you find my library useful.
#5

[eluser]Renea077[/eluser]
Thank you very much for posting this code. This issue has been bugging me for quite some time now. It is great to see that someone found a solution. This will certainly make my life much easier. I tried it out, and it works great! I really appreciate it.
#6

[eluser]Gyorgy Fekete[/eluser]
Smile that's a really old thread.

I haven't been active on CodeIgniter lately. (I switched to Kohana)
So I don't know if this library is still working with CodeIgniter 2

Do the devs. finally added partial views in CI 2?




Theme © iAndrew 2016 - Forum software by © MyBB