1

So I have a Structure like that

  • Item
    • Subitem | inactive
    • Subitem | inactive

why is item.hasDescendants() true ?

How would I do something like that the right way:

<li class="{% if nav_item.hasDescendants() %}more{% endif %}">
2

hasDescendants() is only concerned with whether the entry actually has descendants; not whether they are enabled or not.

To determine in an entry has enabled descendants, you can do this:

{% set hasDescendants = entry.descendants.total() %}

Or in your case:

<li class="{% if nav_item.descendants.total() %}more{% endif %}">

entry.descendants is an alias for EntryModel::getDescendants(), which will return a new ElementCriteriaModel prepped to return descendants of entry. By default it will only return the enabled ones, like all ElementCriteriaModel’s.

| improve this answer | |
  • One would think that hasDescendants() and getDescendants() behave the same ... – maxx Oct 21 '14 at 18:47
  • They serve different needs. hasDescendants will just return a binary true or false depending on whether the entry has any descendants, without any support for parameters that could affect the answer. getDescendants() will actually trigger a DB query, so it can afford to accept parameters, and offer the data in different formats, depending on your needs. – Brandon Kelly Oct 21 '14 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.