Welcome Guest, Not a member yet? Register   Sign In
How to add same content on many page
#1

[eluser]dimethroxy[/eluser]
Ok, let's say I have a website with a box containing the "last 10 comments". I want this box to display on different pages (controller) but not on every page. I don't want to repeat the code for this box in every controller. Is there a way to do this?

I've searched and found the "Modular Extensions - HMVC" but is this the best solution to my problem ?

Thanks and sorry for my English.
#2

[eluser]Pascal Kriete[/eluser]
Hey dimethroxy,

The native way of doing this is to use libraries, helpers, and models. A combination of those can do the job 99% of the time.
In this case it's probably easiest to have a model that contains the query, and then use a a helper (or view) to format it as you like.

So in the page views you just have:
Code:
<?= last_ten_comments() ?>

Welcome to CodeIgniter.
#3

[eluser]ray73864[/eluser]
turn it into a library and have the library autoloaded, then for each page you want to have it on just call the library function (eg. $this->comments->last_10())

inparo beat me to it.... I should really learn to refresh the page if it has been open for a while before posting Smile
#4

[eluser]dimethroxy[/eluser]
Thanks for your reply! After reading the library docs, it seem like my best choice is to create a library that will load my data model and then call the correct view and return the result. Then I will call this library from my view.

Is this correct ?
#5

[eluser]Pascal Kriete[/eluser]
@ray, I did the same thing less than 5 minutes later

@dimethroxy, sounds good to me Smile .
#6

[eluser]dimethroxy[/eluser]
Mmm... stupid questions but how do i load a model from a helper ?
#7

[eluser]Colin Williams[/eluser]
Code:
function helper_function()
{
   $ci =& get_instance();
   $ci->load->model('some_model', 'some');
   $ci->some->do_something();
}




Theme © iAndrew 2016 - Forum software by © MyBB