Fix template field iteration logic