Need help adjusting a template

Hello all.

I’m facing a problem that I can’t solve (due to inexperience, of course) and I’ll try to describe it the best I can.

So, I’m using the Gateway theme and it has a feature where it shows every post from a specific taxonomy type (“featured”) on the front page. This is done through a modular.html.twig template and is a functionality I’d like to keep.

The problem is that the results from other taxonomy types, i.e. posts by author, show up on a page that uses the same template and, since not every post is “featured”, the ones that are not don’t appear.

What I would like to do (if possible) is to adjust the template and make it work like this: if the page is the front page, then show the “featured” posts (default functionality). And if the page is not the front page but one that contains taxonomy results, then show every matching post, “featured” or not.

Alternatively, I would like help in creating a new template that will be used as a base by every page that contains taxonomy results.

As always, I don’t expect someone to come up with a full solution and make it easy for me. Every bit of help or suggestion will be appreciated.

I’m posting the full content of the aforementioned modular template, just for reference:

{% extends 'partials/base.html.twig' %}

{% set featured = page.collection({'items':{'@taxonomy.featured': true},'order': {'by': 'date', 'dir': 'desc'}}) %}

{% block header %}
  {% include 'partials/header.html.twig' %}
{% endblock %}

{% block content %}
<div class="home_posts_titles">
  <div class="row">
    <div class="large-12 columns">
      {{ page.content }}
    </div>
  </div>
</div>
{% if featured %}
<div class="featured-posts">
    {% for row in featured|batch(3) %}
    <div class="row">
    {% for child in row %}
    <div class="large-4 columns">
      {% include 'partials/blog_item.html.twig' with {'page':child, 'truncate':true, 'readmore': false} %}
    </div>
    {% endfor %}
      </div>
    {% endfor %}
</div>
{% endif %}

  <hr>

{% for module in page.collection() %}
{{ module.content }}
{% endfor %}
{% endblock %}
1 Like

One solution would be to pass an option when you include the Twig template. This way you can use the same twig template to render your content, but if you pass a taxonomy type, the called twig can check that and filter the content, else it displays all. Quick pseudo code example:

{% include 'modular/featured.html.twig' with {featured: true} %}

...

{% include 'modular/featured.html.twig' %}

Then just check for this featured variable in your twig template and do something different if it’s set to true.

Thank you very much for your reply.

The thing is that the template I mentioned is not included anywhere and is just used as a base for all modular pages (I think).

What I thought as a possibility was to add something like {% set Posts = page.collection({'items':{'@taxonomy.category': Posts},'order': {'by': 'date', 'dir': 'desc'}}) %} since every post will be included in the “Posts” category and then adjust the if statement by adding {% elseif Posts %} just before the {% endif %} but I don’t know what to add after that or even if it’s a logical thing to do.