This stuff should really go into the preprocess, not into the theme.  In general, logical choices never go in the theme.