Skip to content

Instantly share code, notes, and snippets.

@jzeltman
Last active December 6, 2018 20:13
Show Gist options
  • Save jzeltman/e28b7fa2dfc9088b7c00d2268456d940 to your computer and use it in GitHub Desktop.
Save jzeltman/e28b7fa2dfc9088b7c00d2268456d940 to your computer and use it in GitHub Desktop.
HTL Responsive Images w Lazy Loading for Adobe AEM. Utilizing the lazysizes lazyloading library.
// LazyLoad Library: https://github.com/aFarkas/lazysizes
// Usage
<sly data-sly-use.template="/path/to/template/image.html"
data-sly-call="${ template.imageMarkup @ xsvp=image.xsvpPath, svp=image.svpPath, mvp=image.mvpPath, lvp=image.lvpPath, altText=image.altText, lazyLoad=true }" />
// HTL Template
<template data-sly-template.imageMarkup="${ @ xsvp, svp, mvp, lvp, altText, lazyLoad }">
<picture>
<source media="(max-width: 480px)" data-srcset="${xsvp}" />
<source media="(max-width: 768px)" data-srcset="${svp}" />
<source media="(max-width: 992px)" data-srcset="${mvp}" />
<source media="(min-width: 1400px)" data-srcset="${lvp}" />
<img data-sly-test.lazy="${lazyLoad}" class="lazyload" data-src="${lvp}" alt="${altText}" />
<img data-sly-test="${!lazy}" src="${lvp}" alt="${altText}" />
</picture>
<noscript data-sly-test="${lazy}"><img src="${lvp}" alt="${altText}" /></noscript>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment