[ticket/12891] Replace template loops with 1 loop to reduce generation time

PHPBB3-12891