Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Block/Modules in Code Igniter

So far I've been enjoying using CodeIgniter to create some simple web apps (really just learning the framework), but I can't figure out an easy way to create a block/module.

I would like to have have "Recent Images" block that I can drop on any page on the site without duplicating the query in each page's controller and passing it to the template with $vars.

2 questions:

  1. I am using partials in the views already, but how do I write a partial that pulls from a controller other than the one specified by the url.
  2. How can I cleanly create a controller that pulls the data for that block and does not create a
    page for it.

This seems like it should be simple enough, I'm just not having any luck finding the proper solution on own. Thanks in advance.

like image 830
Rapture Avatar asked Jan 04 '11 16:01

Rapture


3 Answers

Okay, here's my take on "widgets", "modules", "partial views" or whatever you want to call them. Either way we mean reusable items for pages.

There are a bunch of different ways to do this so my way may not be for you - then again it might be just what you're looking for!

Firstly, I make the widgets as views, let's say the paths would be like: views/widgets/myview.php

I make the decision of what widgets I'm planning to use in my controller. My method is to put the paths to the views I want in an array like:

$data['widgets'] = array('widgets/myview', 'widgets/myview2');

Then when I load my main view (still in my controller) I do this:

$this->load->view('main_view', $data);

Now the widgets array is passed to the main view.

Then in my main view when I get to the place where the widgets go, I loop through my array like this:

foreach ($widgets as $widget)
{
    $this->load->view($widget);
}

This loop is run in my main view. Conveniently, all the data you passed to the main view will be available in the nested views you load.

I've made many widgets in Codeigniter this way and it's the method I use in my CMS's as well.

like image 85
sitesbyjoe Avatar answered Sep 29 '22 10:09

sitesbyjoe


what do you mean by partials? There are no native partial views in CodeIgniter. Although, you can $this->load->view() in the views.

If you are talking about variables from the controller that are available for all views that you would load, you should do:

$data['info'] = 'value';
$this->load->vars($data);

$info would then be available to all views you load.

Update:

You need to do that in your constructor:

class Page extends Controller {
    function Page()
    {
        $this->load->model('page');
        $data['images'] = $this->page->get_images();
        $this->load->vars($data);
        // images will now be available to all views
    }

}

Update 2:

I must admit that I haven't used Templates but this would work for all pages created by your controller.

like image 23
Teej Avatar answered Sep 29 '22 09:09

Teej


The good news is that CodeIgniter is definitely moving in this direction. To start with, have a look at Modular Extensions from wiredeignz. It can be found here: https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/overview with good documentation to get you started. This could be perfect for you, because you can run a module (ie call a controller/method of a module and it returns a view partial) and insert it into a template or global view or whatever. Also, look at CI 2.0 - https://bitbucket.org/ellislab/codeigniter/overview It is quite different to earlier versions of codeigniter and has many new features that are very exciting. Although in beta it is very, very stable. HMVC is written to work with CI 2.0.

Also, look at the code for other CI projects that work in this way. I would recommend pyroCMS by Phil Sturgeon. He is really leading the way and has written many interesting articles and posts about modularisation and CI 2.0. www.philsturgeon.co.uk

This way of working is really useful for keeping separate bits of a site independent, only being used when needed.

To try and help you a little more directly...

Don't forget that you can buffer output using the optional third parameter. Look at the user guide if you're not sure.

$partial = $this->load->view('someView', $data, true);
like image 31
musoNic80 Avatar answered Sep 29 '22 10:09

musoNic80