0

I have a StructBlock like this :

class CardBlock(blocks.StructBlock):
header= blocks.StructBlock([
    ("text", blocks.CharBlock(required=False, help_text="Header text")),
    ("classes", blocks.CharBlock(required=False, help_text="Header css classes")),
    ],
    template="streams/card_header_block.html")

image= ImageChooserBlock(required=False)
icon= blocks.CharBlock(required=False, help_text="fontawesome classes for an icon")

title= blocks.StructBlock([
    ("text", blocks.CharBlock(required=False, help_text="Title text")),
    ("classes", blocks.CharBlock(required=False, help_text="Title css classes")),
    ],
    template="streams/card_title_block.html")

bodyHTML = blocks.RawHTMLBlock()

footer= blocks.StructBlock([
    ("text", blocks.CharBlock(required=False, help_text="Footer text")),
    ("classes", blocks.CharBlock(required=False, help_text="Footer css classes")),
    ],
    template="streams/card_footer_block.html")

class Meta:
    template = "streams/card_block.html"
    icon = "placeholder"
    label = "Card"

And a template like this:

{% load wagtailcore_tags %}
{% load wagtailimages_tags %}

{% image value.image fill-300x150 as img %}
<div class="card {% if value.classes %} {{value.classes}} {% endif %}">
    {% if value.header.text is not Null %}
        {% include_block value.header %}
    {% endif %}

    {% if value.image %}
    <img src="{{ img.url }}" alt="{{ img.alt}}" class="card-img-top" />
    {% endif %}

    <div class="card-body">
        {% if value.title %}
            {% include_block value.title%}
        {% endif %}

        {% if value.subtitle %}
            {% include_block value.subtitle%}
        {% endif %}

        <div class="card-text">{% include_block value.bodyHTML %}</div>

        {% if value.link %}
            {% include_block value.link%}
        {% endif %}
    </div>
    {% if value.footer %}
        {% include_block value.footer%}
    {% endif%}
</div>

I am trying to check is a child block like Header has its value filled by the page editor or not. If not I do not show the HTML. But I am afraid the header div still shows up. Someting is wrong with the way I am putting the condition.

shiv
  • 9
  • 3

1 Answers1

0

A blank CharBlock is equal to the empty string, which is not the same as null (and in any case, Python's null value is None, not Null). You can test this the same way that you've tested other empty values in your template: {% if value.header.text %}

gasman
  • 23,691
  • 1
  • 38
  • 56