Fix template bug that occurs when variable is used at the end of a repetition