import jinja2


def format_template(template, *args, **kwargs):
    """
    Format a template, either using jinja2 or str.format().
    Use jinja2 if the template is a jinja2.Template, or contains '{{' or
    '{%'.  Otherwise, use str.format() for backwards compatability with
    old scripts (but you can't mix them).
    :param jinja2.Template | str template:
    :rtype: jinja2.Template | str
    """
    if isinstance(template, jinja2.Template):
        return template.render(*args, **kwargs)
    elif '{{' in template or '{%' in template:
        return jinja2.Template(template).render(*args, **kwargs)
    else:
        return template.format(*args, **kwargs)