djc / askama

Type-safe, compiled Jinja-like templates for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

div appear with its classes initially in case of an Option

HosMercury opened this issue · comments

Hi, For this code

{% match messages %} 
  {% when Some with (msgs) %}
        <div class="bg-red-500 text-white border border-red-700 p-2 rounded-xl">
          {% for msg in msgs %}
          <p class="">{{ msg }}</p>
          <br />
          {% endfor %}
        </div>
        {% when None %} 
{% endmatch %}

Although the messages var is an Option the div classes appear
I mean this part always appears as an empty div with a small height and red background.
it should not appear unless there are messages!

        <div class="bg-red-500 text-white border border-red-700 p-2 rounded-xl">
          {% for msg in msgs %}
          <p class="">{{ msg }}</p>
          <br />
          {% endfor %}
        </div>

Screenshot 2024-02-28 at 1 18 14 AM

I guess msgs is something like Some(&[])? So, even if it's not None, it's empty? Then you have to check for this case {% if !msgs.is_empty() %} … {% endif %}.

although it is of type Option<Messages>

https://docs.rs/axum-messages/0.3.0/axum_messages/struct.Messages.html

But anyhow your hack works

It was my bad
I converted messages to Vec instead of the Option
Checking with is_empty
Worked with empty vec better than Option
As suggested sbove