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:
- 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. - 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.
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:
Then when I load my main view (still in my controller) I do this:
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:
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.