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

PHPBB3-12891
Merge remote-tracking branch 'nickvergessen/ticket/12891' into develop-ascraeus