2. Custom Render Template
"It will help us to append necessary values like sid in render template"
Example of a Custom Render Template Function
/my_flask_app ├── app.py ├── templates │ ├── base.html │ └── index.html └── custom_render.pyfrom flask import render_template import datetime def custom_render_template(template_name, **context): # Add custom context variables context['current_year'] = datetime.datetime.now().year # You can add more custom logic here # Render the template with the updated context return render_template(template_name, **context)from flask import Flask from custom_render import custom_render_template app = Flask(__name__) @app.route('/') def home(): return custom_render_template('index.html', title='Home Page') if __name__ == '__main__': app.run(debug=True)<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>{{ title }}</title> </head> <body> <header> <h1>Welcome to My Website</h1> <p>Current Year: {{ current_year }}</p> </header> <div data-gb-custom-block data-tag="block"></div> </body> </html>{% extends "base.html" %} {% block content %} <h2>This is the Home Page</h2> {% endblock %}
Last updated