Fix logic for deriving class name and cache filename from template name