0

I have a portal with different customers (landlord, tenant)

There is a registration page for them. When they register, I tag them appropriately using roles.

When they log in, the first thing they have to do is fill in their profile. For this, I created a page profile.html...

Both these users have almost similar fields except few. I have some attributes for the landlord and some other for the tenant. But both of them have some similar fields like first_name, last_name, phone, age, sex, etc...

At the moment, I maintain two different profile tables and one profile.html page.

I send them to profile.html and I am using

{% if user == 'landlord' %}
<html
 <body>
     profile pagefor landlord
</body>
</html>
{% endif %}
{% if user == 'tenant' %}
<html
 <body>
     profile pagefor tenant
</body>
</html>
{% endif %}

The problem with the above structure if I am repeating the entire HTML block for each user.

Once a user fills in their profile, I show them read-only profile.html page like

{% if user == 'landlord' and profile_filled %}
<html
 <body>
     read only profile page for landlord
</body>
</html>
{% endif %}
{% if user == 'tenant' and profile_filled %}
<html
 <body>
     read only profile page for tenant
</body>
</html>
{% endif %}

The page profile.html gets too long with these IF's....

Is there a way to simplify this?

vkk07
  • 69
  • 1
  • 10

1 Answers1

1

A common approach to situations like this is to use template inheritance, which separates out the common part into a "base" template. E.g.,

<html>
...
<body>
{% block content %}{% endblock %}
</body>
</html>

And inherit from this base by templates that provide specific content for each of your conditions. E.g., the template that provides content for a landlord who has filled in a profile would look like

{% extends "base.html" %}
{% block content %}
read only profile pages for landlord
{% endblock %}

Then select the appropriate template in the view method by moving the appropriate checks there. Something like

@app.route('/profile')
def profile():
    ...
    if user == 'landlord' and user.has_filled_in_profile():
        return render_template("landlord_with_profile.html", ...)
    elif user == 'tenant' and user.has_filled_in_profile():
        return render_template("tenant_with_profile.html", ...)
    elif ...
Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46