Closed theurbanu closed 2 years ago
You're not showing your full code - what is list
?
Typically, you want to check if there are any items
within a list, not if there's a list itself. Although that does help, but it's not accurate (no list will exist if the user has never added anything, but list
would be true if they have a list but no items in it.
But regardless, I see no issue with that. In fact, using:
{% set list = craft.wishlist.lists().default(true).one() %}
{% if list %}
--- list item info ---
{% else %}
---- custom text ---
{% endif %}
{% if list is empty %}
---- custom text ---
{% else %}
--- list item info ---
{% endif %}
Produces ---- custom text ---
(twice) for a brand new session for a guest. So that seems to work as expected to me?
Sorry I didn't provide everything you needed. So here's the code I have. I have this placed to show the current logged in user what items they have saved. Is this correct?
{% set list = craft.wishlist.lists().type('favoriteListings').one() %}
{% if list %}
{% set items = list.items()
.limit(12) %}
{% paginate items as pageInfo, pageItems %}
{% for item in pageItems %}
----- Item Info (Title, Image, etc) -----
{% endfor %}
{% if pageInfo.totalPages > 1 %}
------Paginate info -----
{% endif %}
{% else %}
----- Text (There are no items saved, etc) ------
{% endfor %}
That looks all correct to me! I get a ----- Text (There are no items saved, etc) ------
notice, because I don't have a list of type favoriteListings
for both a registered user, or a guest.
I've also tested creating the favoriteListings
list type, and it's the same result.
Is that your expected result? Could something else maybe be going on with your project?
Ok, I figured out what the issue is. So it appears if there isn't a List created under the User's account, then the conditional works as it should. However, if the user once had a List (the list is still showing in the database and control panel), but the user removed all of the items from their List, then the conditional does not work and will only show a blank page. So I don't know if maybe this is a bug or am I doing something wrong on my end?
Also, I have it set where only logged in users can save items.
Right, so that's what I was saying before that the check "{% if list %}" will return true if the list exists, even if the list is empty. You probably want to check for both - if the list exists, and is/isn't empty.
list is empty
won't work here because List will be an element object, but it won't work with Twig's is empty
check because that only really works for simple objects like arrays and plain objects. This will be a List object which is complex.
{% set list = craft.wishlist.lists().type('favoriteListings').one() %}
{% if list and list.items() %}
...
{% else %}
----- Text (There are no items saved, etc) ------
{% endfor %}
Ah, I see. I had to adjust it to list.items()|length
and everything now works as should. Thank you so much for your time and help!!
I'm trying to use a simple conditional statement that will display text if there aren't any items in the list. I have tried on the site I'm working on, as well as simple vanilla page, and all that will return is a blank section without any text. I have tried using the 2 different code below. When items are in the list, they both work fine, but if there isn't anything in the list, then it's just blank.
I also tried
Additional info
Additional context