Refactor #cached_templates helper