The Partial Library |
[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> Step three, add some partial views. application/views/featured.php Code: <?php if(isset($featured_articles)): ?> application/views/popular.php Code: <?php if(isset($popular_articles)): ?> 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: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Step seven, create a controller application/controllers/articles.php Code: <?php if( ! defined('BASEPATH')) exit('No direct script access allowed!'); Want a shorthand method? Code: $this->partial->replace(array('left' => 'featured_articles')); How about loading partials into multiple positions? Code: $this->partial->replace(array( Don't want your article to show when you're paginating comments? No problem! Code: if($pagination_offset > 0) 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 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!
[eluser]micha8l[/eluser]
Here's how to avoid name collisions by naming your directories in application/views like a king!! Code: ...
[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: ***********************************************************************
[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.
[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 |
Welcome Guest, Not a member yet? Register Sign In |