In my hubl template I'd like to see if there have been any sub_links added and if so, iterate on them. My issue however is that the hubl tag code (object is the instance that would have the sub_links field):
{{object.sub_links|length}}
This always seems to display a length of "4" anytime I would expect it to be "0" (empty). If I add any items in the CMS, the above code would then show the correct count. For some reason an empty array is displaying a non-zero length. it's also interesting that "4" corresponds with the number of available fields on a single group item (fields on the sub_link item). Is there something wrong with the way I'm using length or setting up the group field? I just want to determine if the group field has contents or not.
Looks like "is iterable" actually did the trick, I just also had an incorrect assumption in my template code. Still confused why the length is inaccurate though...
You're correct, the 'sub_links' group is nested. The 'object' key is just an artifact of trying to consolidate my code for this post 😅 The actual code would be something like this:
{% for link in module.links %}
{% set has_children = link.sub_links is iterable %}
{% if has_children %}
{% set children_class = 'item-has-children' %}
{% endif %}
... template html code ...
{% endfor %}
You can see my mistake was that I was assuming 'has_children' would be destroyed for every iteration of the loop, but it looks like once it gets set it just stays there until explicitly unset. Just so happens that the first 'link' had set 'has_children' to true.
The issue might be that you're missing an id. It can be the same as the name so
"id" : "sub_links",
"name": "sub_links",
..
I'm also not sure why you're using object.sub_links instead of module.sub_links. Is the sub_links field group a nested inside another field group with the id of 'object'?
That doesn't work since it seems the problem is that the sub_links object is being created with an single empty instance (making sub_links iterable) even though the CMS content editor hasn't added anything. Is there an issue with how I've set up the group occurence field that would be causing this?