def evaluate_template(template_name, data):
    global _interpreter
    data = dict(data)
    data['TEMPLATE'] = _evaluate_template
    template_path = template_name
    output = StringIO()
    try:
        _interpreter = em.Interpreter(output=output, options={em.BUFFERED_OPT: True, em.RAW_OPT: True})
        with open(template_path, 'r') as h:
            content = h.read()
        _interpreter.invoke('beforeFile', name=template_name, file=h, locals=data)
        _interpreter.string(content, template_path, locals=data)
        _interpreter.invoke('afterFile')
        return output.getvalue()
    except Exception as e:
        print(f"{e.__class__.__name__} processing template '{template_name}'", file=sys.stderr)
        raise
    finally:
        _interpreter.shutdown()
        _interpreter = None
def evaluate_template(template_name, data):
    global _interpreter
    data = dict(data)
    data['TEMPLATE'] = _evaluate_template
    template_path = template_name
    output = StringIO()
    try:
        _interpreter = em.Interpreter(output=output, options={em.BUFFERED_OPT: True, em.RAW_OPT: True})
        with open(template_path, 'r') as h:
            content = h.read()
        _interpreter.invoke('beforeFile', name=template_name, file=h, locals=data)
        _interpreter.string(content, template_path, locals=data)
        _interpreter.invoke('afterFile')
        return output.getvalue()
    except Exception as e:
        print(f"{e.__class__.__name__} processing template '{template_name}'", file=sys.stderr)
        raise
    finally:
        _interpreter.shutdown()
        _interpreter = None