Reusable modular templates in another templates.
## macros.html
<!-- SECTION 3: LECTURE 16 -->
{% macro render_dict(dictionary) %}
<table>
<tr>
<th>name</th>
<th>value</th>
<th>comments</th>
</tr>
{% for key, val in dictionary.items() %}
<tr>
<td>{{ key }}</td>
<td>{{ val }}</td>
{% if val<= 2 %}
<td>short</td>
{% elif val >= 2 and val <=3 %}
<td>medium</td>
{% else %}
<td style="color: red">long</td>
{% endif %}
</tr>
{% endfor %}
</table>
{% endmacro %}
## using_macros.html
<!-- SECTION 3: LECTURE 16 -->
{% import 'macros.html' as my_macros %}
<html>
<head>
<meta charset="UTF-8">
<title>dictionary data displayed</title>
</head>
<h2>working with macros</h2>
<body>
{{ my_macros.render_dict(movies) }} <br>
</body>
</html>
## macros.py
# python code for SECTION 3: LECTURE 16
# JINJA2 - MACROS
@app.route('/macros')
def jinja_macros():
movies_dict = {'autopsy of jane doe': 02.14,
'neon demon': 3.20,
'ghost in a shell': 1.50,
'kong: skull island': 3.50,
'john wick 2': 02.52,
'spiderman - homecoming': 1.48}
return render_template('using_macros.html', movies=movies_dict)
No comments:
Post a Comment