Load more `if entries` still shows if there are no more to load, and requires one last click to remove the button...
See original GitHub issueQuestion
I have noticed that when testing if there are entries, that the button still shows/appears even if there are no more entries to load. Clicking it again then sets this correctly, but it feels wrong having that last click load nothing more and then the button hides. I believe this should happen on the last element that is loaded. I have posted my code blow, but I also checked the cookbook and it appears the same thing occurs. Is there anyway to set/do this check on each load?
###Code
{% extends "_layouts/global-variables.twig" %}
{% block htmlPage %}
{% set offset = offset ?? 0 %}
{% set entries = craft.entries
.section('blog')
.relatedTo({
targetElement: entryId,
field: 'relatedServices'
})
.with('teaserImage')
.offset(offset)
.limit(limit)
.all() %}
<div class="grid lg:grid-cols-2 gap-x-8 divide-y divide-gray-100 gap-y-8 lg:gap-y-20 lg:divide-y-0 mb-8">
{% for entry in entries %}
{% include '_routers/teasers.twig' %}
{% endfor %}
</div>
{% if entries %}
<button
sprig s-val:offset="{{ offset + limit }}"
s-target="this"
s-swap="outerHTML"
class="p-4 border border-white shadow-md transition ease-in-out duration-200 ///// text-white duration-300 {{ class.btnLinks }}"
>
Load more articles
</button>
{% endif %}
{% endblock %}
To reproduce
Click the load more articles button and it loads 2 more articles (this shows 4 in total - the 2 initially loaded ad the 2 new ones loaded). This is all the articles related. However, the load more articles button still appears until it is clicked again, then it is removed/hidden.
When I check the entries|length
it starts ff as 2, clicking the button it remains at 2, clicking again it is then correctly set to 0.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5
Top GitHub Comments
For reference, I updated the load more recipe to use this technique: https://putyourlightson.com/sprig/cookbook#load-more
Aha. Thanks Ben. I see…