[BUGFIX] Harden layout selector against errors in templates