Welcome Guest, Not a member yet? Register   Sign In
Partial Support for CodeIgniter
#1

[eluser]Jamie Rumbelow[/eluser]
I had to give CI partial support (similar to Rails') for a project, and I'd thought I'd release it here. It's nothing earth-shattering, and only took me a few minutes, but you might appreciate it all the same.

application/libraries/MY_Loader.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package        CodeIgniter
* @author        ExpressionEngine Dev Team
* @copyright    Copyright (c) 2008, EllisLab, Inc.
* @license        http://ellislab.com/codeigniter/user-guide/license.html
* @link        http://codeigniter.com
* @since        Version 1.0
* @filesource
*/

// ------------------------------------------------------------------------

/**
* Partial Loader
*
* Gives Partial Support to CI
*
* @package        CodeIgniter
* @subpackage    Libraries
* @author        Jamie Rumbelow <http://www.jamierumbelow.net>
* @category    Loader
* @link        
*/
class MY_Loader extends CI_Loader {

  function partial($name, $pass = null, $object = "object") {
    
    if (is_null($pass)) {
    
      $this->view($name);
      
    }
    if (is_array($pass)) {
    
      foreach ($pass as $ob) {
        $this->view($name, array($object => $ob));
      }
      
    }
    elseif (is_object($pass)) {
      
      $this->view($name, array($object => $pass));
      
    }
  
  }
  
}

As you may see, you can pass through an array, which will then loop the number of items in the array and set the variable in the partial (which you can name in the third parameter, otherwise it will be called $object) to the value of that item. This is handy for stuff like blog posts, where you can have the output in the partial and then not worry about looping through your posts in the view. This also keeps your views less cluttered.

Enjoy!
#2

[eluser]xwero[/eluser]
A few things to think about :

- why make the method only usable in a view file because it can only output directly

- why write $this->load->patrial('view') if $this->load->view('view') is the same

- why force developers to pass an object to add variables to a view if the view method preferred way is using an array, and don't allow the object properties to be extracted if the developer passes an object

- why loop views if loading one view and looping an array is faster

- not code wise, why do you add the comment about CodeIgniter to your MY_ file, your extended class is not part of the CI framework
#3

[eluser]Jamie Rumbelow[/eluser]
Okay...

1. Because that is the point of partials - they are basically sub-views
2. It's not the same. $this->load->partial(); does a similar action, but the added functionality (of looping through the array)
3. You can pass an array and it will loop through it. You can pass an object and it will be an object. I repeat, this IS NOT A MIRROR OF THE VIEW FUNCTION. This was made to give a basic Rails-like partial support to CodeIgniter. If this is to much for you, then go in a hole somewhere and drown.
4. Because it makes views cleaner, and some people prefer the use of partials.
5. Because it's a third party library, and thus can still be counted as "part of CodeIgniter" it is an extension to it.

I'm sorry but that was downright rude. If you don't see the point in it then ignore it.
#4

[eluser]xwero[/eluser]
[quote author="Jamie Rumbelow" date="1236531386"]If this is to much for you, then go in a hole somewhere and drown.
[/quote]
And i am rude one here Smile
#5

[eluser]xwero[/eluser]
I only compare it with the behavior of the view method because it's an extension of the loader class and it is the method that has the most similarities with your method. You actually use the view method internally.

If you extend a class you have to be aware of the way things are done in the parent class. If you made a plugin out of your method it's a stand alone functionality and you are free to define the behavior.

Adding the codeigniter comment can be seen as riding on the reputation of the framework to elevate the trust level of your code. in your response you wrote yourself it's a third party library, which by definition is not a part of the framework.

My remarks were meant as constructive criticism in general.
#6

[eluser]meteor[/eluser]
man
$this->load->view('controller/partials/partial',params_array(),true);
and you have partials ......
#7

[eluser]johnwbaxter[/eluser]
Quote:If this is to much for you, then go in a hole somewhere and drown.

I'm quite actually properly surprised at that.

@Jamie Rumbelow - I dunno if you ever come across xwero in the forums and see his posts or not. I do quite a lot and find him to be incredibly helpful and give excellent advice (he hasn't spent his nearly 4000 posts being an asshole to people) and be a real asset to this community. He was just trying to start a conversation with you about your work which could have been useful to you and anyone wanting to use your contribution as it would have shown more reasoning behind your code and it's application.

There is no need in the world to say that to him. None at all.




Theme © iAndrew 2016 - Forum software by © MyBB