Best way to render dynamic data
Hi guys,
I've been using Fuel extensively over the last months and am wondering about what the best / most efficient practice is to render module-data in the front-end?
The data gets rendered in either a layout, view or page file.
In my opinion there are 3 good ways:
a)
Define a custom rendering function in the module's library and return an HTML string, like:
public function render_html() {
...
return $html_stringl;
}
and call it in the layout/view:
echo $this->fuel->MODULE_NAME->render_html();
b)
Create a static block in MODULE_NAME/views/_blocks and call it in the layout / view, like:
echo fuel_block(array('view' => ‘BLOCK_NAME’, 'module' => ‘MODULE_NAME’));
c)
Define a public get_XXX() - function the module's model and call it, like:
$article = fuel_model('MODEL_NAME', array('find' => 'one', 'where' => array('slug' => $slug)));
$article->XXX
I would love to hear your opinion on this topic because it keeps nagging me for ages.
Thanks a lot,
Julius
Comments
Most of the time, we will use blocks and either pass in the data or simply include the fuel_model function call at the top of the block to simplify things.
With the scenario where the you have say a model record, let's say an "invoices_model" for this example, and you want to tie view specific information to a particular record's value (e.g. perhaps a rendering of the invoice), you could tie it to the model record as a get_invoice_html method. This makes sense because you are dealing with an object that contains all the information you need to render the invoice.
The scenario where you attach it to the module's library, the Fuel_blog in the blog module does that for generating rss feeds:
https://github.com/daylightstudio/FUEL-CMS-Blog-Module
Yesterday we built a YouTube video module for a client and implented the different video views as you said via blocks loading the model data.
I'll upload it to GitHub as soon as its finished.