Correct way to make data available to blocks?

edited June 2012 in News & Announcements
Hi - I am a bit of a newbie - so forgive the basic question:

I want to include a (sidebar) panel of data on a few of my website pages (shows relationships between users and status info) and I want to have flexibility over which pages this appears on (will change over time). To me this sounds like a good application of a block.

The examples of blocks (e.g. quotes and projects) use modules - which I think I understand. However the data I am considering is data that is inherent to my application and not anything that I need to expose in the CMS and so I don't think a module is appropriate (correct me if I'm wrong).

My question is : what is the best practice way of exposing data to my block view?
- It doesn't have it's own controller as it is (dynamically) included into other views
- I could directly call the relevant model but this sounds like it would be a no-no in MVC terms
- I could make the data available to every view, but this feels inefficient

I understand this is probably more an MVC/codeigniter question, but it is the use of blocks that has triggered the problem for me.

Thanks in advance for any guidance you can give

Comments

  • edited 3:34PM
    There is a "vars" parameter you can use to pass variables to the block like so:
    <?=fuel_block(array('view' => 'my_block', 'vars' => array('my_var1' => 'my_var_value1')))??
  • edited 3:34PM
    Thanks, but I guess my question is more basic... where would I setup that variable. If for example I want to send an array of data that is the result of a function in a model?
  • edited 3:34PM
    We normally will use the view itself to generate the data results and then pass it to the block (as in above). You could create a controller that performs that logic, then passes it to the view, then passes it to the block, but that is one of those extra steps that we don't mind breaking the normal MVC rules for.
  • edited 3:34PM
    do you mean calling the model directly from the view?
  • edited 3:34PM
    Most of the time we'll use fuel_model to get model data from within the block.
    // from within views/_blocks/my_block.php $data = fuel_model('my_model'); foreach($data as $key => $val): echo $val->my_prop; endforeach;
  • edited 3:34PM
    Thanks - but I thought this was to access modules that have been built to extend fuel cms functionality. The model I want to access is one that I don't want to expose in the CMS as a module - can I still use fuels_model to access a 'standard' model?
  • edited 3:34PM
    You are right in that to use fuel_model you need have it be a CMS module. You can set the "hidden" module property in the MY_fuel_modules which will hide in from the navigation. However, the module still is accessible through the URL. So, the better alternative may be to use the following:
    $CI->load->model('my_model'); data = $CI->my_model->find_all(); ....
  • edited 3:34PM
    Great - thanks for your help
Sign In or Register to comment.