# Buggy code
def _render(template, render, renderer, template_dict, opts):
    """
    Render a template
    """
    if render:
        if template_dict is None:
            template_dict = {}
        if not renderer:
            renderer = opts.get("renderer", "yaml_jinja")
        rend = salt.loader.render(opts, {})
        blacklist = opts.get("renderer_blacklist")
        whitelist = opts.get("renderer_whitelist")
        ret = compile_template(
            template, rend, renderer, blacklist, whitelist, **template_dict
        )
        ret = ret.read()
        if str(ret).startswith("#!") and not str(ret).startswith("#!/"):
            ret = str(ret).split("\n", 1)[1]
        return ret
    with salt.utils.fopen(template, "r") as fh_:
        return fh_.read()


# Traceback (SaltRenderError)
sudo salt -G 'role:saltmaster' state.apply test.http-template saltenv=dev
saltmaster-prod1:
----------
ID: https://domain.tld/api
Function: http.query
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1750, in call
**cdata['kwargs'])
File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1705, in wrapper
return f(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/states/http.py", line 90, in query
data = __salt__['http.query'](name, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/modules/http.py", line 33, in query
return salt.utils.http.query(url=url, opts=__opts__, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/utils/http.py", line 194, in query
data_file, data_render, data_renderer, template_dict, opts
File "/usr/lib/python2.7/dist-packages/salt/utils/http.py", line 765, in _render
ret = compile_template(template, rend, renderer, blacklist, whitelist, **template_dict)
File "/usr/lib/python2.7/dist-packages/salt/template.py", line 101, in compile_template
ret = render(input_data, saltenv, sls, **render_kwargs)
File "/usr/lib/python2.7/dist-packages/salt/renderers/jinja.py", line 70, in render
**kws)
File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 178, in render_tmpl
output = render_str(tmplstr, context, tmplpath)
File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 386, in render_jinja_tmpl
buf=tmplstr)
SaltRenderError: Jinja variable 'tpl_name' is undefined
Started: 20:09:03.041707
Duration: 6.336 ms
Changes:

# Fixed code
def _render(template, render, renderer, template_dict, opts):
    """
    Render a template
    """
    if render:
        if template_dict is None:
            template_dict = {}
        if not renderer:
            renderer = opts.get("renderer", "yaml_jinja")
        rend = salt.loader.render(opts, {})
        blacklist = opts.get("renderer_blacklist")
        whitelist = opts.get("renderer_whitelist")
        ret = compile_template(
            template, rend, renderer, blacklist, whitelist, **template_dict
        )
        if salt.utils.stringio.is_readable(ret):
            ret = ret.read()
        if str(ret).startswith("#!") and not str(ret).startswith("#!/"):
            ret = str(ret).split("\n", 1)[1]
        return ret
    with salt.utils.fopen(template, "r") as fh_:
        return fh_.read()