Junior — Middle
Can you give an example of using a template in an application server?
sobes.tech AI
Answer from AI
In the application server, templates are often used to generate HTML pages with dynamic content. For example, in Python with Flask and Jinja2, you can create an HTML template where data from the server is inserted:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
user = {'name': 'Ivan'}
return render_template('index.html', user=user)
The index.html file:
<!DOCTYPE html>
<html>
<head><title>Example</title></head>
<body>
<h1>Hello, {{ user.name }}!</h1>
</body>
</html>
Thus, the template allows separating application logic from presentation and conveniently inserting data into HTML.