Show message IF no posts

Hi. So I’m trying to show a message when there’s not posts (children) under a page. Here’s my code:

{% if not page.find('/insights').children %}
<p>There are not posts</p>
{% else %}
<div id="post-list" class="flex-container grid">
   {% for post in page.find('/insights').children.order('date', 'desc').slice(0, 3) %}
   <div class="col-4">
      <a class="post-list--item" href='{{post.url}}'>
         <header>
            {% for tag in post.taxonomy.tag %}
            <span class="tag {{ label_style ?: 'label-secondary' }} p-category" href="{{ blog.url|rtrim('/') }}/tag{{ config.system.param_sep }}{{ tag }}#body-wrapper">{{ tag }}</span>
            {% endfor %}
            <h2 class="post-item--title">{{ post.title }}</h2>
         </header>
         <footer>
            <span class="post-item--date">{{ post.date|date("M j, Y")}}</span>
            <span class="post-item--arrow">
            </span>
         </footer>
      </a>
   </div>
   {% endfor %}
</div>
{% endif %}

Now for some reason it works in a way that the posts do only show when there’s child pages but the notification doesn’t show up when there’s none. What am I doing wrong?

You could try:

{% if page.find('/insights').children|length == 0 %}

Or:

`{% if not page.find('/insights').children|length %}`

PS I didn’t test this I must admit.

@eeroma, Some background:

  • According Grav’s docs on Page.children():

    children() : \Grav\Common\Page\Collection
    Returns children of this page.

    When you print {{ var_dump(page.children) }} the result will be shown as being of type object.

  • According the docs of Twig about truthy/falsy values, an object evaluates to true.

Considering the above, not page.children will always evaluate to false.

Not sure if this is helpful, but we recently wrote a similar bit for page collections:

{% block content %}
        <!-- START: under_construction.html.twig -->
        {% if collection|length < 1  %}
            <div class="main-content">
                <div class="container-fluid ml-0 mr-0">
                    <div class="row ml-0 mr-0">
                        <div class="col-1  col-md-2">&nbsp;</div>
                        <div class="col-10 col-md-8 blog-main">
                            <h3 class="mt-5 mb-5">There is nothing to show under this category </h3>
                        </div>
                        <div class="col-1  col-md-2">&nbsp;</div>
                    </div>
                </div>
            </div>
        {% else %}
            <div class="main-content">
                <div class="container-fluid ml-0 mr-0">
                    <div class="row ml-0 mr-0">
                        {% for child in collection %}
                            <div class="col-12 blog-main"> <!-- 2-8-2 enforced in child content -->
                                {% include 'partials/under_construction_item.html.twig' with {'blog': page, 'page': child, 'truncate': true} %}
                            </div>
                        {% endfor %}
                    </div>
                </div>
            </div>
        {%  endif %}
    {% endblock %}