Embedded HTML cleanups to improve readability

* Move template context retrieval to the top of the code block,
* Use format() to avoid mixing string with variables,
* Use implicit line continuation instead of endlessly concatenating strings,
* Use print as a function.

This a previous step to strip out the HTML into proper external templates.