Welcome Guest, Not a member yet? Register   Sign In
The Partial Library
#1

[eluser]micha8l[/eluser]
Partial Library will help you develop your application faster, no need for template engines here!

Step one, copy and paste the library to application/libraries/Partial http://pastebin.com/wqNQBeE1

Step two, create a layout file.
application/views/layout_three_column.php
Code:
<!DOCTYPE html>
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Three Columns&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

<! -- position left --&gt;
<div>
&lt;?php $partial = $this->partial->load('left'); ?&gt;
&lt;?php foreach($partial as $key => $value); ?&gt;
&lt;?php echo $value; ?&gt;
&lt;?php endfor; ?&gt;
</div>

&lt;!-- position right --&gt;
<div>
&lt;?php $partial = $this->partial->load('right'); ?&gt;
&lt;?php foreach($partial as $key => $value); ?&gt;
&lt;?php echo $value; ?&gt;
&lt;?php endfor; ?&gt;
</div>

&lt;/body&gt;
&lt;/html&gt;

Step three, add some partial views.
application/views/featured.php
Code:
&lt;?php if(isset($featured_articles)): ?&gt;
<h2>Featured Articles</h2>
&lt;?php echo $featured_articles; ?&gt;
&lt;?php endif; ?&gt;

application/views/popular.php
Code:
&lt;?php if(isset($popular_articles)): ?&gt;
<h2>Popular Articles</h2>
&lt;?php echo $popular_articles; ?&gt;
&lt;?php endif; ?&gt;

Step four, modify the library $uri property to match your default controller!
application/libraries/Partial.php
Code:
$this->uri = ($uri)? $uri : 'home';

Step five, add your partial view paths to $this->views
application/libraries/Partial.php
Code:
$this->views = array('featured', 'popular');

Step six (optional), create config.php - views will be loaded from config file
application/config/partial.php
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config = array(
'articles' => array(
    'left' => array(
        'featured',
     ),
     'right' => array(
         'popular',
     ),
),
'articles/latest' => array(
    'left' => array(
        'featured',
     ),
     'right' => array(
         'popular',
     ),
));
/* End of file partial.php */

Step seven, create a controller
application/controllers/articles.php
Code:
&lt;?php if( ! defined('BASEPATH')) exit('No direct script access allowed!');

class Articles extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->library('partial');

        // $articles = $this->articles_model->latest_articles();
        $featured_articles = '<p>Suas simul oblique ne eum, cum atqui paulo accumsan te.</p>'
        $popular_articles = '<p>Lorem ipsum dolor sit amet, partem explicari eum isit.</p>'

        $this->partial->replace(array(
            'left' => array(
                'featured_articles' => $featured_articles,
            ),
            'right' => array(
                'popular_articles' => $popular_articles,
            ),
        ));

        $this->load->view('layout_three_column');
    }

    public function latest()
    {
        $this->load->view('layout_three_column');
    }
}

Want a shorthand method?
Code:
$this->partial->replace(array('left' => 'featured_articles'));

How about loading partials into multiple positions?
Code:
$this->partial->replace(array(
    'left' => 'featured_articles',
    'right' => 'popular_articles',
    ),
));

Don't want your article to show when you're paginating comments? No problem!
Code:
if($pagination_offset > 0)
{
    $this->partial->trim_begin('left');
}

What's that you say? User is not logged so you want to remove a partial?
Code:
if( ! $this->session->userdata('logged_in')) // or something to that extent
{
    $this->partial->remove(array(
        'left' => array(
            'featured',
        ),
    ));
}

I'm actively developing this class, I'll update regularly, I've got it working in production. I remove the "die()" statement as these are for development. When I get the time I'll intergrate it with CI's ENVIRONMENT constant.

I'm thinking about implementing a role based access system for the $views property too. Suggestions welcome!


Messages In This Thread
The Partial Library - by El Forum - 03-08-2012, 11:49 PM
The Partial Library - by El Forum - 03-09-2012, 12:04 AM
The Partial Library - by El Forum - 03-09-2012, 12:26 AM
The Partial Library - by El Forum - 03-09-2012, 05:30 AM
The Partial Library - by El Forum - 03-09-2012, 12:34 PM
The Partial Library - by El Forum - 03-09-2012, 11:40 PM
The Partial Library - by El Forum - 08-09-2012, 07:06 PM



Theme © iAndrew 2016 - Forum software by © MyBB