def expand_template(template: str, **kwargs: str) -> str:
    """Expand the template by substituting the arguments.

    If the argument contains multiple lines then extra spaces are added to each
    line to preserve the correct indentation.

    """
    # Add spaces to each line of some multi-text input
    def add_spaces(num, text):
        space = " " * num
        out = []
        for idx, line in enumerate(text.splitlines()):
            if idx == 0:
                out = [line]
            else:
                out.append(space + line)
        return "\n".join(out)

    if not template:
        return ''
    lines = template.expandtabs(4).splitlines()
    outlines = []
    for line in lines:
        start = line.find("{%")
        if start == -1:
            outlines.append(line)
            continue
        end = line.find("%}", start)
        if end == -1:
            raise ValueError("Bad template expansion in {line}")
        keyword = line[start+2:end]
        text = add_spaces(start, kwargs[keyword])
        line = line[0:start] + text + line[end+2:]
        outlines.append(line)
    return "\n".join(outlines)