0

I am creating a web app that allows users to create timelines (e.g. for worldbuilding or novels).

Within this, for a historical event the user can enter these 8 things: Start year/month/day/hour, and end year/month/day/hour, with only Start year being required.

The app inserts these into a database along with the rest of the entry details, into two columns: start date which contains the start y/m/d/h and end date which contains end y/m/d/h. These strings are formatted like so: 'Y:M:D:H'. Sometimes they might be '203:None:None:None' or '203:None:23rd:None' etc.

I have come to displaying them within my web app with jinja, and so far the options I have to display them are very long and will just be a very large amount of ifs/elifs.

To show what I am aiming for, here is the first part of code I have:

note: sy=start year, ey=end year, sm=start month, and so on and so forth. These are retrieved via sy,sm,sd,sh=era_entries_dict[era][5].split(':'). This code is within a for loop over a list of eras, and displays historical entries within eras.

{% if (sm == 'None') and (sd=='None') and (sh=='None') %}
    {% if ey == 'None' %}
        <h6 class="text-muted">{{ sy }}</h6>
    {% else %}
        {% if (em=='None') and (ed=='None') and (eh=='None') %}
            <h6 class="text-muted">{{ sy }} > {{ ey }}</h6>
        {% elif (ed=='None') and (eh=='None') %}
            <p class="text-muted">{{ sy }} > {{ sm }} {{ sy }}</p>

So essentially, if its only the years on their own (sy or sy and ey) it will be in a h6 tag, else a paragraph tag.

Is there any way to do this shorter? I appreciate any tips/advice. Thanks!

hc199
  • 11
  • 6
  • 1
    There's no way to avoid that. See: https://stackoverflow.com/questions/15168831/how-to-write-a-multiline-jinja-statement Another way is to create a flag in your python controller, and pass the boolean flag as a variable to your jinja template. – knh190 May 31 '19 at 01:21
  • 2
    If you started to think that you wrote too much template code, you know that it is time to refactor that logic into your views. – Selcuk May 31 '19 at 02:51

0 Answers0