Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Show message IF no posts

Started by Eero 6 years ago · 3 replies · 447 views
6 years ago

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

TWIG
{% 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?

6 years ago

You could try:

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

Or:

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

PS I didn't test this I must admit.

6 years ago

@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.

last edited 06/16/20 by pamtbaau
6 years ago

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

HTML
{% 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 %}
last edited 06/17/20 by Duane A Nickull

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 56 13 hours ago
Support · by Anna, 3 days ago
2 61 16 hours ago
Support · by Justin Young, 17 hours ago
1 32 17 hours ago
Support · by Duc , 1 week ago
2 66 5 days ago
Support · by Colin Hume, 1 week ago
2 59 5 days ago