import re


def LoopTemplate(s, ctx):
    """Minute templating language: use like str.format,
       so LoopTemplate("{one}", dict(one="1")) => "1",
       but also supports {loop people}{name}{endloop} which
       loops over an iterable in the context and processes the
       inside as a subtemplate with the next item in the iterable
       as its context."""
    def loophandler(m):
        md = m.groupdict()
        return "".join([LoopTemplate(md["content"], val)
                        for val in ctx[md["var"]]])
    return re.sub(r"\{loop (?P<var>[^}]+)\}(?P<content>.*?)\{endloop\}",
                  loophandler, s, flags=re.DOTALL).format(**ctx)