Creating a simple view helper in Flight
Monday, June 18th, 2012 | Limited, Programming
Recently I wrote about the Flight PHP micro-framework. One of the challenges I ran into when launching one of our sites in it was that we had some database information that needed to be pulled out in the layout.
It was the same information on every page so it didn’t make sense to pass it in as a variable as this would have meant using the same code on every route to do the same thing. Instead, it was simpler to create a view helper which would pull out the information needed.
class Helper {
public static function menuTags () {
$tagModel = Flight::tagModel();
return $tagModel->findDistinct();
}
}
Nothing fancy here, just a static method on a class which hooks into one of the model classes I has already registered with Flight and returns the result. I then just needed to reference this from the layout view.
<?php foreach (Helper::menuTags() as $tag) { ?>
<?=htmlspecialchars($tag["tag"])?>
<?php } ?>
Now it appears on every page without me having to pass it in as a variable.
Recently I wrote about the Flight PHP micro-framework. One of the challenges I ran into when launching one of our sites in it was that we had some database information that needed to be pulled out in the layout.
It was the same information on every page so it didn’t make sense to pass it in as a variable as this would have meant using the same code on every route to do the same thing. Instead, it was simpler to create a view helper which would pull out the information needed.
class Helper { public static function menuTags () { $tagModel = Flight::tagModel(); return $tagModel->findDistinct(); } }
Nothing fancy here, just a static method on a class which hooks into one of the model classes I has already registered with Flight and returns the result. I then just needed to reference this from the layout view.
<?php foreach (Helper::menuTags() as $tag) { ?> <?=htmlspecialchars($tag["tag"])?> <?php } ?>
Now it appears on every page without me having to pass it in as a variable.