-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lazyload images #1131
Comments
you will probably need to create a plugin that implements a Twig filter or function. Have a look at some of the other plugins available in the Grav downloads section that are similar in function. |
Thx for your answer but i'm just a frontend dev ;) |
I'll take a look at lazysizes and see what I can do. |
Can you explain more what you need? It looks like all you have to do is load the JS and use raw IMG tags instead of markdown. If you want to do this theme wide, just add the JS in the base partial. I can write a plugin that would load the JS on a page by page basis, but the best way to use this would still be to use raw IMG tags instead of markdown. To do a twig extension would require knowing more what you're trying to accomplish. |
@Perlkonig yes it would be possible with simple images, but i need to change attributes names on responsive images that's why i'm using Grav derivatives and sizes built-in methods. {% set services_image = page.media.images|first.derivatives(640, 1024, 200).sizes('(min-width:1024px) 1024px, 100vw').cropZoom(1024,170) %} |
With the improvements to derivatives, it might be possible to create a plugin that takes the output generated by Grav's derivatives, and with regex, reformat it into the appropriate lazyload format. I say this is probably the best approach because the derivatives are already generating the images, and really all you need is a slightly tweaked output. |
Here's the PR that was just merged: #1107 |
Having no experience with images derivatives, not sure how much I can help. I'll take a look at least. |
Maybe the great @fredrikekelund could take a look at it ? ;) |
You really don't need a plugin for this, just mark up your image with normal HTML, and manually set the {% set services_image = page.media.images|first.derivatives(640, 1024, 200).sizes('(min-width:1024px) 1024px, 100vw').cropZoom(1024,170) %}
<img class="lazyload" data-src="{{ service_image.url(false) }}" data-srcset="{{ service_image.srcset(false) }}"> |
Once again @fredrikekelund killed it ;) |
Good solution 👍 |
@julienchazal no worries! |
@fredrikekelund tiny correction to your awesome code
You set |
Here's my solution. Credit to Ricardo from the Grav Discord chat for the help. Instead of making Grav add its srcset images into the data-srcset attribute, change the Lazysizes Javascript so that it utilizes srcset instead. I'm also using the Lazysizes Blur Up plugin along with it: Twig/HTML/CSS:
Load the JS in the Head:
|
Hello
I'm currently implementing image lazyloading with lazysizes
Image attributes
src
must be replaced withdata-src
, andsrcset
withdata-srcset
How can i achieve this with this twig rendering code please ?
The text was updated successfully, but these errors were encountered: