Condition based on page.collection.modular|length


I’ve set-up and have a loop that displays modular content on a page. All good up to here.
But I wanted to enhance the user experience by showing a “nothing available” should there be no modular content for this loop.
And some how I feel like I’m missing something but it’s not working.
As you can see in my code, I added a vardump() to make sure the count is correct. And it is.
On a page where there is no modular content, it dumps int(0). And when there are, the it dumps the correct count.
But the following condition does not work. Does anybody know what am I not doing correctly ??
Here is my code.

{% set totalmodule = page.collection.modular|length %}
<h2>{{ vardump(totalmodule)}}</h2>
{% for module in page.collection() if module.template == 'modular/suite' %}
  {% if totalmodule >= 1 %}
    {{ module.content }}
  {% else %}
    <h2>No suites available for this property</h2>
  {% endif %}
{% endfor %}

Not sure what you mean by “The following condition does not work”.

What are your seeing?:

  • Nothing is printed in the for-loop?
    Are you sure you enter the for-loop? In other words, does “page.collection() if module.template == ‘modular/suite’” returns any modules?
  • Or the loop is running, but the condition {% if totalmodule >= 1 %} is evaluated incorrectly and hence the wrong text is printed in the for-loop.

Hi pamtbaau,

indeed, sorry about that, I should have been more explanatory.
What I meant by “The following condition does not work”, is that the message “No suites available for this property” does not show when the “else” is meant.
even if I do {% if totalmodule == 0 %} or {% if totalmodule < 1 %}
although the vardump shows int(0).
And as I said, on pages where there are modular content, the vardump returns the correct count. ie.: int(3).

I hope I manage to be clearer. English is a second language to me. So sometimes it’s not easy to be clear for something technical in a different language :slight_smile:

@julien A few more questions:

  • How do you know the code inside the for-loop is reached? Have you vardump-ed the value of ‘totalmodule’ inside the for-loop? Does it print anything?
  • How does define the collection?
  • What does the folder structure look like?

How do I know the for-loop is reached?
for pages that have modular content, the modular content prints correctly and the vardump ‘totalmodule’ is printing the correct number of visible modular content.

How does define the collection?
in my I have the following in the header/frontmatter

items: ‘@self.modular

NB: the tab indetation is correct on my code.

What does the folder structure look like?

I’m starting to wonder if I should not simply use child pages as opposed to modular content. And have these child pages not extending the base template so only their content is printed.
I’m still a bit consufed on the best approach for this.

I might start coming across as nagging… But ask you anyway:

  • How do you know the code inside the for-loop is reached when the value of ‘totalmodule’ is zero?

Why do I keep asking?

  • The collection only contains modular pages.
  • If the collection is empty (totalmodule == 0) then the subset of type ‘modular/suite’…
    page.collection() if module.template == 'modular/suite
    …will also be empty. In that case, the for-loop will not be entered and “No suites available for this property” will never be printed.

I might not fully understand your intension, but if you want to print a message when no suites are available, the following code seems more logical to me:

{% set collection = page.collection.ofType('modular/suite') %}
{% if (collection|count) >= 1 %}
  {% for module in collection %}
      {{ module.content }}
  {% endfor %}
{% else %}
    <h2>No suites available for this property</h2>
{% endif %}

wow @pamtbaau !
First, I’m really sorry I did not get what you meant right away. Now with your reasoning, it is more clear. And you are absolutely right. Obvisouly it would not print the h2 tag !
I guess I did not think of the subtility between |length and |count .

And thanks a lot, that works as intended on my part.