views:

2178

answers:

10

In CodeIgniter or core PHP is there an equivalent of Rails's view partials and templates?

A partial would let me render another view fragment inside my view, like have a common navbar.php view that I could point to inside my homepage.php view. Templates would let define the overall shell of an HTML page in one place, and let each view just fill in the main body section.

The closest thing I could find in the CodeIgniter documentation was Loading multiple views where several views are rendered sequentially in the controller, but it seems strange to be dictating the visual look of my page inside the controller... i.e. to move the navbar my designer would have to edit the controller.

Searching on Stack Overflow for a PHP way to accomplish this, I found this page which talks about simulating partials with ob_start. Is that the recommended approach inside CodeIgniter?

A: 

Symfony does this with their partial/component setup.

Peter Bailey
A: 

This is unfortunately not CodeIgniter specific at all, however I suggest you to have a look at Savant3 template system. It allows you to render the template to a string. Then you can simply stuck it to wherever you wish.

Maybe there's something like that in CodeIgniter?

I can think of a way to add a facility when rendering the view, to render the template, and then all the sub-template it contains.

Elazar Leibovich
+3  A: 

In PHP you'd use include

troelskn
A: 

CodeIgniter and Smarty play nicely together.

GloryFish
+2  A: 

this is essentially what I use:

function render_partial($file, $data = false, $locals = array()) {
$contents = '';

foreach($locals AS $key => $value) {
 ${$key} = $value;
}

${$name . '_counter'} = 0;
foreach($data AS $object) {
 ${$name} = $object;

 ob_start();
 include $file;
 $contents .= ob_get_contents();
 ob_end_clean();

 ${$name . '_counter'}++;
}

return $contents;

}

this allows you to call something like:

render_partial('/path/to/person.phtml', array('dennis', 'dee', 'mac', 'charlie'), array('say_hello' => true));

and in /path/to/person.phtml have:

<?= if($say_hello) { "Hello, " } ?><?= $person ?> (<?= $person_counter ?>)

this is some magic going on though that may help you get a better picture of what's going on. full file: view.class.php

mike wyatt
extract($locals);
Mario
jeej i tkRenderPartial() a lot myself ;) +1
Ropstah
+9  A: 

I may be breaking some MVC rule, but I've always just placed my "fragments" in individual views and load them, CodeIgniter style, from within the other views that need them. Pretty much all of my views load a header and footer view at the top and bottom, respectively:

<? $this->load->view( "header" ); ?>
//Page content...
<? $this->load->view( "footer" ); ?>

The header could then include a NavBar in the same fashion, etc.

Brent
This is the way to go for partials not requiring their own data. But in the case of partial views requiring data ("widgets"), something like the 3rd party "Modular Extensions HMVC" is a good choice.
rick
+1  A: 

I found myself moving from Rails to CI too, and what I did with partials is basically render the partials in the view as a variable and set it from the controller.

So in the view you would have something like (_partial.php):

<h2>Here Comes The Partials</h2>
<?= $some_partials ?>

And you can set it from the controller like:

$this->load->view('the_view', 
   array('some_partials', 
         $this->load->view('_partial', array(), TRUE)
   )
);

Personally, I prefer to use CI's view instead of ob_start, but that's me =) PS: When loading views, first argument is the view name, second one is the parameters to be passed to the view, and the third one is "ECHO" flag, which basically tells CI whether to render it directly or return the value of the view instead, which is basically what I did in the example.

I don't think it's a good solution though, but it works for me. Anyone has better solutions?

Ferry
A: 

Here is a blog post describing how to use smarty with codeigniter. The asp.net like masterpage is also implemented.

Niran
+1  A: 

My recently released Template library works in this way.

That template library also plays nicely with my Dwoo implementation which will give your views much more power.

Phil Sturgeon
A: 

Try out Ocular - http://codeigniter.com/wiki/Ocular_Layout_Library/ - its a rails inspired templating library.

Geshan