optimize dict iterations in template