Variable that is automatically calculated in each Action / View


I wondering what the best way is to make a variable available for all views / actions. Let’s say, for example, we have a site like Facebook, which has the notifications in its header. The notifications need to be checked for new ones and eventually updated on each request. What’s the best way to implement this in Yii? What I did so far is calculating it in the layout, which has the drawback of the variable not being available in the $content (I assume the $content is rendered first). Any other ideas?

Thanks in advance!