jinja2 macros intro
+24 −23 · 2 files changed
-
templates/macros.html+16 −0 -
templates/weather-average.html+8 −23
@@ -0,0 +1,16 @@ +{% macro weather_cell(value,hl) %} + <td> + {% if hl %}<b>{% endif %} + {{ value }} + {% if hl %}</b>{% endif %} + </td> +{% endmacro %} + +{% macro weather_row(month, min, max, rain, hl_min, hl_max, hl_rain) %} + <tr> + <td>{{ month }}</td> + {{ weather_cell(min, min <= hl_min) }} + {{ weather_cell(max,max >= hl_max) }} + {{ weather_cell(rain,rain >= hl_rain) }} + </tr> +{% endmacro %} \ No newline at end of file @@ -1,3 +1,4 @@ +{% import 'macros.html' as macros %} <h1>Weather Averages</h1> <h2>{{ city }}</h2> <hr> @@ -9,28 +10,12 @@ <th>Rainfall</th> </tr> {% for month in months %} - <tr> - <td>{{ month }}</td> - <td> - {% set hl = weather[month]['min'] <= highlight['min'] %} - {# set a variable hl, to a condition if, number to show is less - row equal to highlight minimum #} - {% if hl %}<b>{% endif %} - {{ weather[month]['min'] }} - {% if hl %}</b>{% endif %} - </td> - <td> - {% set hl = weather[month]['max'] >= highlight['max'] %} - {% if hl %}<b>{% endif %} - {{ weather[month]['max'] }} - {% if hl %}</b>{% endif %} - </td> - <td> - {% set hl = weather[month]['rain'] >= highlight['rain'] %} - {% if hl %}<b>{% endif %} - {{ weather[month]['rain'] }} - {% if hl %}</b>{% endif %} - </td> - </tr> + {{ macros.weather_row(month, + weather[month]['min'], + weather[month]['max'], + weather[month]['rain'], + highlight['min'], + highlight['max'], + highlight['rain']) }} {% endfor %} </table> \ No newline at end of file