views:

32

answers:

2

Hey guys,

I have this library in CI that retrieves my latest twitter updates. It has a function that sends my latest updates as objects to my controller.

I would like to show these twitter updates on the footer of my page, so they're visible at all times.

Now my question is how I call these directly from a view? I know this is not a good practice in MVC but I don't see how else I could do this. My controller currently takes care of all my different pages (it's a small website) and I don't think it's very good practice to call my twitter class at the end of every page-function in the controller and then send it through to the views.

Typycally I do this in my controller:

<?php
    function index(){
    $data['page'] = 'home';
    //i don't want to call my twitter class here every single time I write a new page. (DRY?!)
    $this->load->view('template', $data);
    }
?>

And it loads the "template" view that looks like this:

<?php
$this->load->view('header');

$this->load->view('pages/'.$page);

$this->load->view('footer');
?>

So any suggestions how I should do this?

+2  A: 

I have a helper library that takes a page Partial and wraps it in the master theme. You can use the optional parameter on your load->view to render to a string.

Then when you render your master page, you can load the twitter updates, and display them. Although, I highly suggest caching your twitter response for 5 minutes at least, will save you a LOT of overhead.

Example:

// Controller somwhere:
$content = $this->load->view('pages/'.$page, array(), true);
$this->myLibrary->masterPage($content);


// Your library:
function masterPage($content)
{

    $twitterData = $this->twitter->loadStuff(); // whatever your function is

    $twitter = $this->load->view('twitter_bar', array('data' => $twitterData), true);

    $this->load->view('master', array('content' => $content, 'twitter' => $twitter);
}
Aren
Thanks, did it this way. Having a helper that loads the view is a nice idea.
Sled
:) Thanks, glad I could help. I never understood why a master-page idea was never baked into CI. Always boggled my mind.
Aren