Welcome Guest, Not a member yet? Register   Sign In
Easier way to include repeating bits of code in several controllers/views
#1

[eluser]Unknown[/eluser]
Dear Everybody,

I've been igniting all day and I've just p**ped out a newsletter signup system (with token activation) in no time. Codeigniter ftw!

Now I'm working on this project for a customer who wants the sidebar of every page to contain repeating "blocks"/bits of code. Let's say we have a newsletter signup form, a random quote from a customer and a button linking to the contact page. Let's say we have a home page, a news page and an about page. Home should have the signup form, the random quote and the button, whereas the about page should only have a random quote and a button. You understand that these are just simple examples and could be just as easy copy-pasted in the views. But there are several, and more pages, so not having to copy paste would be nice.

Where I am so far:
I've made a view for each "block" that I need. Each controller has a variable (array) containing the names of the view files, located in views/blocks. I made a "block helper", with just one simple function that loops through the array and includes the view files. This worked, until I had to add some data from a database.
Selecting data in the view that is included with the function described above, using $this->db-> does not work, because you are not in the object anymore.
I messed around a little bit, but can't seem to find a way around having to copy paste every bit in the view files. I'm not a pro in OOP, so it might be a n00b question. Apologies in advance.

Question:
Is there a way to do database querying inside of an included view file? And/or is there an easier way to prevent copy pasting and repeating html code by using some sort of "blocks", while being able to assign specific blocks to each controller/page?

* may be noteworthy: I'm loading views with a Template library. So in the controller I load a template (and tell it what view to load). The function that includes "blocks" is located in the view files (that are loaded in the template).


Thanks alot!
#2

[eluser]Unknown[/eluser]
I'm sorry u guys, I just rearranged some stuff and now load the "blocks" using the load->view function. Works like a breeze. Wanted to delete post, but I can't Tongue




Theme © iAndrew 2016 - Forum software by © MyBB