CodeIgniter Forums
How I render partials? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: How I render partials? (/showthread.php?tid=9622)



How I render partials? - El Forum - 07-01-2008

[eluser]Martin Snajdr[/eluser]
There is my code!

Everything you need to add to every partial a function where have to define the array of variables and load the class (better is autoload).

I am going to add a function for rendering master page/pages

Hope it help you.

libraries/Partial.php
Code:
<?php  
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Partial
{
    function Partial()
    {
        $this->obj =& get_instance();
    }
    
    function view($partial)
    {
        $data = $this->$partial();
        $this->obj->load->view('partials/'.$partial, $data);
    }
    
    function first()
    {
        $data['hello'] = "Hello";
                        
        return $data;
    }
    
    function second()
    {
        $data['simple'] = "It's simple";
        
        return $data;
    }    
}
?>

controller/Main.php
Code:
<?php

class Main extends Controller {

    function Main()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $this->load->view('index');
    }
}

views/index.php
Code:
<h3>This is my index, guys!</h3>
    
&lt;?=$this->partial->view('first')?&gt;

&lt;?=$this->partial->view('second')?&gt;

views/partials/first.php
Code:
<h3>This is my first partial, guys!</h3>
&lt;?=$hello?&gt;

<br />

views/partials/second.php
Code:
<h3>This is my second partial, guys!</h3>
&lt;?=$simple?&gt;

<br />



How I render partials? - El Forum - 07-02-2008

[eluser]xwero[/eluser]
It's an interesting way of making it easier to render a page but i think in the real world it will be only useful for a limited number of partials. A way to up the number of partials would be to put your library in the system/libraries and create a MY_Partials.php file in the application/libraries directory. This splits the data methods from the actual library methods.

I think there better ways to do this but it could be useful for someone.


How I render partials? - El Forum - 07-02-2008

[eluser]Martin Snajdr[/eluser]
[quote author="xwero" date="1214998120"]It's an interesting way of making it easier to render a page but i think in the real world it will be only useful for a limited number of partials. A way to up the number of partials would be to put your library in the system/libraries and create a MY_Partials.php file in the application/libraries directory. This splits the data methods from the actual library methods.

I think there better ways to do this but it could be useful for someone.[/quote]

Thanks for answer xwero,

That's great idea but many people, including me, don't like to add any files into the system folder. So what about extending the Partial.php with a model where will be the data methods?


How I render partials? - El Forum - 07-02-2008

[eluser]xwero[/eluser]
If you have models and views in a library you should think about creating a controller because you are creating a pseudo controller. But the 'bad' thing about controllers in CI is that there is no elegant way call them in other controllers. Then you have to rely on 'big' libraries like Matchbox an ME but the developer of Matchbox made the Wick library that provides you an elegant way to load controllers.

In the long run it is a more transparent way of segmenting the code than adding data calls to a library.