Having this logic in the view is a bit odd.  Would there be easy to way to push this to a controller or object?