I’m just starting out with python and since I’m coming from a PHP background (thus have to adjust my mind to wrap around new concepts) one major question still stands without answer – why does Flask need a template engine?
The way I was used to go about things, was to have a template.html
that contained valid html without any logic and then, when needed, just load the file into a DOMDocument
object and navigate it using DOMXpath
. This made perfect sense to me – leave the UI part separate from the LOGIC part.
But now since I’m starting to learn a new language, it kinda forces me to get back to mixing in logic with the UI. So it boils down to this – is it me, is the DOM and xpath route just something I do to make my life “harder”, or is Flask wrong to use a template engine?
1
There’s absolutely nothing wrong with using a template engine as long as it’s completely focused on display logic. Looping over lists, printing fields from records, formatting strings – all of this is fine. You still have a clearly defined boundary between where you’re “doing things” and where you are simply formatting it for display. What you shouldn’t do is actually be modifying or, even worse, generating the data in your display code. The point is to make it simple to find where any piece of data comes from or is modified & make it simple to modify one part without the other.
With an ideal separation of concerns, the same application logic could be used, unmodified, to generate your site, do a total redesign of your HTML, generate a mobile version, XML/JSON exports or any other possible representation. What you were doing before seems to directly tie your application logic to the structure of your template – the exact opposite of “don’t mix logic and UI” is trying to accomplish – an absurdly literal and dogmatic application of a general rule.
1