@staticmethod
def _extract_macros_from_template(template, env):
    """Take a template string and extract any macros from it.

        Lovingly inspired by http://codyaray.com/2015/05/auto-load-jinja2-macros
        """
    from jinja2.runtime import Macro
    context = {}
    macro_template = env.from_string(template)
    for k in macro_template.module.__dict__:
        attr = getattr(macro_template.module, k)
        if isinstance(attr, Macro):
            context[k] = attr
    return context
