Variables

In this lesson, we will get to know the method of passing variables to the template and using them with Jinja!

Introduction

Flask allows us to pass any Python object to the template, which can then be referred to inside the template using Jinja syntax.

How does it work?

We can use variables inside the templates by following these two steps:

  1. The object is provided as a named argument to the render_template() function.
return render_template("index.html", my_object = Object)
  1. The value of this object is then fetched inside the template using {{}} syntax.
{{ my_object }}

📌 Note: Multiple parameters can be passed to the template using the method mentioned above.

String example

Consider a string variable called Username. We can access its value inside the template using the following steps:

Get hands-on with 1200+ tech skills courses.