Related posts with else block

Hello. I have a block of related posts on a post page by tag. I would like to not display anything at all if the current post contains a tag that is not in other posts. The official documentation says that you can use {{else}} inside {{get}}, but it doesn’t work

{{#get "posts" include="tags" limit="3" filter="id:-{{comment_id}}+tag:{{primary_tag.slug}}" as |related|}}

<section class="related-posts">

    <h3 class="related-posts__title">Related posts</h3>

    <div class="related-posts__inner content-width content-width--medium">

        {{#foreach related}}

        <div class="related-posts__item related-item">

            <a class="related-item__link" href="{{url}}">

                {{!-- Post title --}}

                <h2 class="related-item__title">{{title}}</h2>

            </a>

            <div class="related-item__meta post-meta">

                <span class="related-item__reading-time">{{reading_time minute="1 мин" minutes="% мин"}}</span>

                <span class="related-item__date"> - Опубликовано {{date published_at timeago="true"}}</span>

            </div>

        </div>

        {{/foreach}}

    </div>

</section>

{{else}}

Nothing

{{/get}}

Hey @leokolt,

You don’t need {{else}} if you want to output nothing.

{{#get "posts" include="tags" limit="3" filter="id:-{{comment_id}}+tag:{{primary_tag.slug}}" as |related|}}
    ...
{{/get}}

This already filters posts by the primary tag, so if there’s no post with same tag, nothing will be output.

But how to hide the entire layout if there are no posts? I still have a block visible on the page with the Related posts heading and an empty inner

[image]

Can you do something like #if related And wrap the heading with that conditional?

1 Like

It turns out that you can. I tried it and everything works out. You need to insert {{#if related}} right after {{get …}}. Great. Thanks!

1 Like