Sergey Blohin
Sergey Blohin

Reputation: 620

How to ignore a custom attribute when rendering flask render_template?

I have some HTML template, for example:

<span data-test-id="fullName">{{ user.full_name }}</span>

↓ in dev/qa environment

<span data-test-id="fullName">Homer Simpson</span>

I want to ignore custom attribute data-test-id in production environment.

<span data-test-id="fullName">{{ user.full_name }}</span>

↓ in dev/qa environment

<span>Homer Simpson</span>

Is it possible?

Upvotes: 0

Views: 53

Answers (1)

PGHE
PGHE

Reputation: 1962

You could use the FLASK_ENV value as a switch and pass it into your render_template call as a kwarg. You'll want to look into Jinja2 for more powerful functionality.

Flask App

import os
...

@app.route('/')
def your_route():
    ...
    environment = os.environ.get("FLASK_ENV", 'dev')
    return render_template('your.html', environment=environment)

HTML

<span {% if environment != 'production'%} data-test-id="fullName" {% endif %}>{{ user.full_name }}</span>

Upvotes: 1

Related Questions