Flask Jinja include



.
├── app.py
├── test_app.py
└── templates
    ├── incl
    │   ├── footer.html
    │   └── header.html
    └── main.html


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

@app.route('/')
def main():
    languages = [
        {
            'name': 'Python',
            'year':  1991,
        },
        {
            'name': 'JavaScript',
            'year':  1995,
        },
        {
            'name': 'C',
        }
    ]
    return render_template('main.html',
        title     = 'Code Maven Jinja include example',
        languages = languages,
    )

examples/flask/jinja-include/templates/main.html
{% include 'incl/header.html' %}


<h2>Languages</h2>
<ul>
  {% for lang in languages %}
     <li>{{ lang.name }}
        {% if lang.year %}
             {{ lang.year }}
        {% else %}
             Timeless
        {% endif %}
     </li>
  {% endfor %}
</ul>

{% include 'incl/footer.html' %}

examples/flask/jinja-include/templates/incl/header.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes">
    <title>{{ title }}</title>
</head>
<body>
<h1>{{ title }}</h1>

examples/flask/jinja-include/templates/incl/footer.html
</body>
</html>

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

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

    rv = web.get('/')
    assert rv.status == '200 OK'
    assert b'<title>Code Maven Jinja include example</title>' in rv.data
    assert b'<h1>Code Maven Jinja include example</h1>' in rv.data
    assert b'Timeless' in rv.data