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!
#2

[eluser]skunkbad[/eluser]
You're joking, right?
#3

[eluser]micha8l[/eluser]
Here's how to avoid name collisions by naming your directories in application/views like a king!!

Code:
...
'articles/_forms/comment',
'articles/_partials/list_categories',
'articles/_partials/list_featured',
'articles/_partials/list_popular',
'articles/_partials/list_drafts',
'articles/_partials/recently_viewed',
'articles/article',
'articles/article_draft',
'articles/articles',
'articles/comments',
...
#4

[eluser]InsiteFX[/eluser]
He's gota being joking!
#5

[eluser]micha8l[/eluser]
I understand that you guys think, "why not just load views in views?" and "why not just load multiple views from the controller?" This class won't benefit from simple layouts, but for generating complex layouts like below, combined with a CSS framework such as Blueprint or the 960 Grid System, then you'll see the benefits fast.

BluePrint CSS Framework

Code:
***********************************************************************  
* Header                                                              *  
*                                                                     *  
*                                                                     *  
***********************************************************************  
                                                                        
********************* ***************** *******************************  
*                   * * Centre        * * Right                       *  
* Left              * *               * *                             *  
*                   * *               * *                             *  
*                   * *               * *                             *  
*                   * *               * *                             *  
*                   * *               * *                             *  
********************* *               * *******************************  
                      *               *                                  
********************* *               * *************** ***************  
*                   * *               * * Right 1     * * Right 2     *  
* Left 1            * *               * *             * *             *  
*                   * *               * *             * *             *  
*                   * *               * *             * *             *  
*                   * *               * *             * *             *  
*                   * *               * *             * *             *  
********************* ***************** *************** ***************  

***********************************************************************  
* Footer                                                              *
*                                                                     *  
*                                                                     *  
*                                                                     *  
***********************************************************************
#6

[eluser]skunkbad[/eluser]
[quote author="micha8l" date="1331321692"]I understand that you guys think, "why not just load views in views?" and "why not just load multiple views from the controller?" This class won't benefit from simple layouts, but for generating complex layouts like below, combined with a CSS framework such as Blueprint or the 960 Grid System, then you'll see the benefits fast.[/quote]

I guess I just think that what you have done adds such a big layer of complexity to such a simple task. I use nested views all the time, and have never came across a situation where I thought I needed anything that CI didn't provide. Everyone has their own programming style, and I think your library reflects yours. There's nothing wrong with that, if that's the way you like to work, but I think it would make things more difficult for me, and that's not something that I can afford to deal with, and I really wouldn't want to deal with it, so it's just not for me. My first impression when looking at your code was, "Damn, I could have done what I need to do, to do what he is doing, in like 10 seconds". I'm being a little sarcastic of course.
#7

[eluser]micha8l[/eluser]
Added the ability to remove views and re-factored the code.

If anyone feels like, putting this on Git for me, I'd greatly appreciate it!

Feel free to email for latest file michael [dot] t [dot] rich [at] gmail [dot] com

New Link http://pastebin.com/wqNQBeE1




Theme © iAndrew 2016 - Forum software by © MyBB