ComputerGeek
search
What Is Flask render_template() Function? Updated: Mar. 13 2026 | Created: Mar. 13 2026

What Is Flask render_template() Function?

In Flask, the render_template() function is used to render an HTML template file and return the final HTML output to the web browser.

Flask uses the Jinja template engine to process template files. These template files can contain a mixture of HTML and Jinja template code such as variables, loops, and conditions.

When the render_template() function is executed:

  1. Flask loads the specified template file from the templates directory.
  2. The Jinja template engine processes the template code.
  3. The function returns a fully rendered HTML string that the web browser can display.

If the template file contains only pure HTML, the render_template() function simply returns the HTML code without modification.

Examples of Flask render_template() Function

Example 1: Rendering a Static Template

In this example, the template file contains only static HTML content.

index.html

<!DOCTYPE html>
<html>
<head>
    <title>Demo</title>
</head>
<body>
    <h1>Hello, World!</h1>
</body>
</html>

app.py

from flask import render_template

render_template('index.html')

Result

<!DOCTYPE html>
<html>
<head>
    <title>Demo</title>
</head>
<body>
    <h1>Hello, World!</h1>
</body>
</html>

Since the template contains only HTML, the render_template() function returns the file content exactly as it is.

Example 2: Rendering a Dynamic Template

In this example, the template contains Jinja variables, making the output dynamic.

index.html

<!DOCTYPE html>
<html>
<head>
    <title>Demo</title>
</head>
<body>
    <h1>Hello, {{ name }}!</h1>
</body>
</html>

app.py

from flask import render_template

render_template('index.html', name='John')

Result

<!DOCTYPE html>
<html>
<head>
    <title>Demo</title>
</head>
<body>
    <h1>Hello, John!</h1>
</body>
</html>

In this example, the Jinja variable {{ name }} is replaced with the value "John" before the final HTML is sent to the browser.

Syntax of Flask render_template() Function

from flask import render_template

render_template(template_name_or_list, **context)

Parameters of Flask render_template() Function

template_name_or_list (str | Template | list[str | Template])

This parameter specifies the template file to render.

Examples:

Flask automatically looks for these files inside the templates directory of your project.

**context (Any)

The **context parameter allows you to pass variables to the template.

These variables can then be accessed inside the template using Jinja syntax.

Examples:

render_template('index.html', name='John', age=21)

Inside the template:

<p>Name: {{ name }}</p>
<p>Age: {{ age }}</p>

Return Type of Flask render_template() Function

str

The render_template() function returns a string containing the final rendered HTML.

At this point, all Jinja template code has already been processed, so the browser only receives standard HTML.

FAQs About Flask render_template() Function

When should you use the Flask render_template() function?

You should use the render_template() function whenever you want Flask to render an HTML template file and return it as a response to the browser.

It is commonly used in Flask route functions.

Example:

@app.route('/')
def home():
    return render_template('index.html')

Can render_template() render template strings?

No. The render_template() function is designed to render template files stored in the templates directory.

If you want to render a template string directly in Python code, you should use the render_template_string() function instead.

Related Flask Topics

You may also want to learn about: