Many frameworks have the concept of βpartial,β which are commonly used for this kind of thing.
Since these partial files are usually read-only and often displayed on every page (or in a specific set of pages), you can access them without thinking from the point of view of controllers like you for the page.
In other words - remember that this is great if your view / layout code speaks directly to your models, if it only interrogates them.
I've been doing this all this time:
layout.php:
<div id="newsWidget"> <?PHP $news = Model_News::latest(); ?> <?PHP foreach($news as $newsitem): ?> <?PHP endforeach; ?> </div>
Model_News :: latest () may implement some caching, etc., since this is the layout code, and I probably don't want to hit db on every request.
No need to inflate the controller
source share