putyourlightson / craft-sprig

A reactive Twig component framework for Craft CMS.
https://putyourlightson.com/plugins/sprig
MIT License
124 stars 9 forks source link

How to use "once" to lazy load content? #7

Closed Joobs closed 4 years ago

Joobs commented 4 years ago

I'm still getting my head around htmx/sprig. I want to create some lazy loaded elements on the page.

I've tried the following, but the once parameter does not seem to be respected, it will continuously poll the template, so I think i've misunderstood how this is meant to be done.

Parent:

{{ sprig('_components/_sprigIframe') }}

_components/_sprigIframe:

<div class="" sprig s-trigger="load once">
  {% if not sprig.request %}
  loading...
  {% else %}
  My iframe code
  {% endif %}
</div>

Thanks

bencroker commented 4 years ago

You’ll want to put the load element inside the condition, see the example at https://putyourlightson.com/sprig-cookbook#dynamic-content.