Flask - calling in the templating engine
Archive - Originally posted on "The Horse's Mouth" - 2015-10-11 11:47:47 - Graham EllisIn previous steps, we have set up flask [here] and done some routing [here]. Let's now define our view.
Our view is in HTML (and with style and lots of other stuff later) and the template will be produced by an HTML expert and graphic artist, and not by a programmer. It will be in a separate place (actally a folder called "templates" to start with, and will be called up by another method in flask which says "fill in this template":
@app.route("//")
def greeting(folder,word):
return render_template('person.html',surname=folder.capitalize(),
forename=word.capitalize())
render_template needs to be loaded, so the code will also include:
from flask import render_template
and you'll start seeing some controller code within the main file, as we use Python to manipulate teh incoming data prior to passing it across to the rendering engine. Complete application code [here] and it renders like this:

But of course that's not the full story. How did the text get placed and thr other text added? Where was the format defined? That was all in the template - that file called person.html in the templates folder. You can find the source [here]. It includes:
<title>About {{forename}} {{surname}}</title>
and
<h1>All (or some) about {{forename}} {{surname}}</h1>
which are our place holders and our first use of the Jinja2 templating system.