title: jinja2.exceptions.UndefinedError: 'lgi' is undefined
body: <p>I have a query that appears to work, doesn't throw an error.
  I get an 'lgi' is undefined error when calling the value?</p>
  
  <pre><code>def search_staff():
      sql_staff = """SELECT s.forename, s.surname, s.brc, s.oou_id, s.staff_id, s.status, lgi.staff_id
                      FROM staff s
                          JOIN local_grant_investigators as lgi ON s.staff_id = lgi.staff_id
                      GROUP BY s.forename, s.surname, s.brc, s.oou_id, s.staff_id, s.status, lgi.staff_id;
      """
      r_s = list(conn.execute(text(sql_staff)))
  </code></pre>
  
  <pre class="lang-html prettyprint-override"><code>{% if staff %}
  &lt;table&gt;
      &lt;thead&gt;
          &lt;th&gt;Surname&lt;/th&gt;
          &lt;th&gt;Forename&lt;/th&gt;
          &lt;th&gt;Status&lt;/th&gt;
          &lt;th&gt;Cluster&lt;/th&gt;
          &lt;th&gt;TEST Cluster ID&lt;/th&gt;
          &lt;th&gt;&lt;/th&gt;
          &lt;th&gt;&lt;/th&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;         
          {% for s in staff %}                    
          &lt;tr&gt;                
          &lt;td&gt; {{ s.surname }} &lt;/td&gt;
          &lt;td&gt; {{ s.forename }} &lt;/td&gt;
          &lt;td&gt; {{ s.status }} &lt;/td&gt;   
          &lt;td&gt; {{ s.staff_id }} &lt;/td&gt;                                 
          &lt;!-- &lt;td&gt;   --&gt;             
            {% if s.staff_id %}          
          &lt;td&gt;
              {{ lgi.staff_id }}
          &lt;/td&gt;
            {% endif %}                           
          &lt;!-- &lt;/td&gt;   --&gt;                                
          &lt;td&gt;                 
          &lt;/td&gt;                   
          &lt;td&gt;&lt;a data-toggle="modal" href="#editstaff" class="staffedit" id="{{s.staff_id}}|{{s.forename}}|{{s.surname}}"&gt; Edit &lt;/a&gt;&lt;/td&gt;   
          &lt;!--    &lt;td&gt;&lt;a data-toggle="modal" href="#removestaff" class="staffremove" id="{{s.staff_id}}|{{s.forename}}|{{s.surname}}"&gt; Remove &lt;/a&gt;&lt;/td&gt;  --&gt;   
          &lt;/tr&gt;               
          {% endfor %}                        
          &lt;/tbody&gt;
  &lt;/table&gt;
  {% endif %}
  </code></pre>
  
  <pre class="lang-python prettyprint-override"><code>Traceback (most recent call last):
    File "C:\Program Files\Python36\lib\site-packages\flask\app.py", line 1997, in __call__
      return self.wsgi_app(environ, start_response)
    File "C:\Program Files\Python36\lib\site-packages\flask\app.py", line 1985, in wsgi_app
      response = self.handle_exception(e)
    File "C:\Program Files\Python36\lib\site-packages\flask\app.py", line 1540, in handle_exception
      reraise(exc_type, exc_value, tb)
    File "C:\Program Files\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
      raise value
    File "C:\Program Files\Python36\lib\site-packages\flask\app.py", line 1982, in wsgi_app
      response = self.full_dispatch_request()
    File "C:\Program Files\Python36\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
      rv = self.handle_user_exception(e)
    File "C:\Program Files\Python36\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
      reraise(exc_type, exc_value, tb)
    File "C:\Program Files\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
      raise value
    File "C:\Program Files\Python36\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
      rv = self.dispatch_request()
    File "C:\Program Files\Python36\lib\site-packages\flask\app.py", line 1598, in dispatch_request
      return self.view_functions[rule.endpoint](**req.view_args)
    File "C:\Program Files\Python36\lib\site-packages\flask_login\utils.py", line 228, in decorated_view
      return func(*args, **kwargs)
    File "C:\inetpub\wwwroot\FlaskClive\theapp\routes.py", line 1003, in search_staff
      return render_template('search_staff.html', staff=r_s)
    File "C:\Program Files\Python36\lib\site-packages\flask\templating.py", line 134, in render_template
      context, ctx.app)
    File "C:\Program Files\Python36\lib\site-packages\flask\templating.py", line 116, in _render
      rv = template.render(context)
    File "C:\Program Files\Python36\lib\site-packages\jinja2\asyncsupport.py", line 76, in render
      return original_render(self, *args, **kwargs)
    File "C:\Program Files\Python36\lib\site-packages\jinja2\environment.py", line 1008, in render
      return self.environment.handle_exception(exc_info, True)
    File "C:\Program Files\Python36\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
      reraise(exc_type, exc_value, tb)
    File "C:\Program Files\Python36\lib\site-packages\jinja2\_compat.py", line 37, in reraise
      raise value.with_traceback(tb)
    File "C:\inetpub\wwwroot\FlaskClive\theapp\templates\search_staff.html", line 1, in top-level template code
      {% extends "base.html" %}
    File "C:\inetpub\wwwroot\FlaskClive\theapp\templates\base.html", line 102, in top-level template code
      {% block content %}{% endblock %}
    File "C:\inetpub\wwwroot\FlaskClive\theapp\templates\search_staff.html", line 67, in block "content"
      {{ lgi.staff_id }}
    File "C:\Program Files\Python36\lib\site-packages\jinja2\environment.py", line 430, in getattr
      return getattr(obj, attribute)
  jinja2.exceptions.UndefinedError: 'lgi' is undefined
  </code></pre>