I avoid duplicating this code block (lines 266-280) I suggest that you create a macro http://jinja.pocoo.org/docs/templates/#macros