Flask Jinja template dictionary



examples/flask/jinja-dictionary/app.py
from flask import Flask, render_template
app = Flask(__name__)

@app.route("/")
def main():
    person = {
        'fname' : 'Mary',
        'lname' : 'Ann',
        'email' : 'mary-ann@example.com',
    } 
    return render_template('main.html',
        title     = 'Person',
        person    = person,
    )

examples/flask/jinja-dictionary/templates/main.html
<h1>{{ title }}</h1>

<h2>{{ person.fname}} {{ person.lname }}</h2>
<table>
  {% for key in person.keys() %}
     <tr><td>{{ key }}</td><td>{{ person[key] }}</td></tr>
  {% endfor %}
</table>

examples/flask/jinja-dictionary/test_app.py
import app

def test_app():
    web = app.app.test_client()

    rv = web.get('/')
    assert rv.status == '200 OK'
    assert b'<h1>Person</h1>' in rv.data
    assert b'<h2>Mary Ann</h2>' in rv.data
    assert b'<td>Mary</td>' in rv.data