Introduction
Flask is a python based web framework that allows the creation of a web application in python language with the help of its features and tools.
When a website is under development, Its code can be divided into two categories,
The Presentation Logic
The Business Logic
The Presentation logic is the code that governs how the browser will display the website, and the Business logic controls all the functionalities the website will perform.
As an example, let us consider a banking application. The homepage consists of a background, buttons, navigation bar, headers, tables, heading, paragraphs, and images. These components are created using HTML and CSS and are under the presentation logic. All the functionalities come under the business logic when a user login-logout is performed.
In flask, it is possible to return HTML elements. However, this would mean having both presentation and business logic reside in the same file.
This is where Templates are helpful. Instead of returning HTML elements inside the business logic file, we return HTML files directly with the help of the render_template() function.
Furthermore, with Jinja templating language, it is possible to embed if-else statements and loops into an HTML file that provides dynamic functionality to a website.
Templates
Now that we have a little understanding of Templates, it is time to concentrate our grasp on their use with the help of a practical example.
Example
Consider the app.py file below,
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return '<html>
<body>
<h1>Hello User</h1>
<h2>Welcome to Coding Ninjas !</h2>
</body>
</html>'
if __name__ == '__main__':
app.run(debug = True)This code renders a Hello User message when the user is in the root route. Now, what if there are multiple routes on a website. The code will become highly cumbersome.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return '<html>
<body>
<h1>Hello User</h1>
<h2>Welcome to Coding Ninjas !</h2>
</body>
</html>'
@app.route('/home')
def index():
return '<html>
<body>
<h1>Hello User</h1>
<h2>Welcome to Coding Ninjas Home Page !</h2>
</body>
</html>'
@app.route('/contact')
def index():
return '<html>
<body>
<h1>Hello User</h1>
<h2>Welcome to Coding Ninjas Contact Page !</h2>
</body>
</html>'
@app.route('/faq')
def index():
return '<html>
<body>
<h1>Hello User</h1>
<h2>Welcome to Coding Ninjas FAQ Page !</h2>
</body>
</html>'
if __name__ == '__main__':
app.run(debug = True)Returning HTML tags for each route in the same file makes the code look complicated and overwhelming.
Templating
To solve the above problem, we use the render_template(). The render_template function allows us to attach an HTML file instead of writing the entire HTML code in the file.
To use render_template, we need to import it first.
from flask import render_template
Let us create a folder with the name templates in the application directory. This folder will house all the .html files that the render_template function will embed.
In our case let us create a welcome.html file inside the templates folder.

welcome.html
<!doctype html>
<html>
<body>
<h1>Hello User</h1>
<h2>Welcome to Coding Ninjas !</h2>
</body>
</html>
app.py
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/')
def message():
return render_template(“welcome.html”)
if __name__ == '__main__':
app.run(debug = True)Output:

The above code is less complex and is easy to debug. Using templates were able to separate our presentation logic from the business logic.




