Skip to content

Commit

Permalink
updated responsiveImage include
Browse files Browse the repository at this point in the history
updated readme
  • Loading branch information
CreateSean committed Jan 27, 2025
1 parent f1a06dc commit 4cfbd2b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 84 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ The layout template has a commented out header tag with that when enabled will m

This uses an include to generate a picture element with multiple sources with webp falling back to jpeg. use this example code to add an image to any page

if the image is empty uses a **fallback image** from **placeholder.com** - this can be update on line 21 of `_includes/responsiveImage.twig`
if the image is empty uses a **fallback image** from **placeholder.com** - this can be update on line 5 of `_includes/responsiveImage.twig`

```twig
{{ include('_includes/responsiveImage', {
Expand Down Expand Up @@ -317,6 +317,10 @@ if the image is empty uses a **fallback image** from **placeholder.com** - this
}
},
],
figureClass: null,
figureStyle: null,
pictureClass: null,
caption: null,
attributes: {
alt: image.altText ?? image.title ?? null,
class: '',
Expand Down
32 changes: 4 additions & 28 deletions templates/_blocks/hero/standardHero.twig
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
}
},
],
figureClass: null,
figureStyle: null,
pictureClass: null,
caption: null,
attributes: {
alt: image.altText ?? image.title ?? null,
class: 'object-cover z-0 w-full h-full lg:min-h-[450px] md:min-h-[375px] min-h-[300px]',
Expand All @@ -44,34 +48,6 @@
},
}, with_context = false) }}

{# {% set transforms = {
'(max-width: 767px)': {
width: 767,
height:300,
mode: 'crop',
quality: 80,
},
'(max-width: 1199px)': {
width: 1200,
height:375,
mode: 'crop',
quality: 80,
},
'(min-width: 1200px)': {
width: 1920,
height:450,
mode: 'crop',
quality: 80,
},
} %}
{% set attributes = {
class: [
'object-cover z-0 w-full h-full lg:min-h-[450px] md:min-h-[375px] min-h-[300px]',
],
loading:'lazy',
alt: heroImage.altText ?? heroImage.title ?? null,
} %}
{{ craft.images.pictureMedia(heroImage, transforms, null, attributes) }} #}

{# hero content #}
<div class="absolute top-0 left-0 z-10 w-full h-full py-8">
Expand Down
4 changes: 4 additions & 0 deletions templates/_includes/news-listing-article.twig
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
}
},
],
figureClass: null,
figureStyle: null,
pictureClass: null,
caption:null,
attributes: {
alt: image.altText ?? image.title ?? null,
class: '',
Expand Down
110 changes: 55 additions & 55 deletions templates/_includes/responsiveImage.twig
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
{#
// set some default transforms that can be overridden in the template - this isn't done yet
{%- set transforms = transforms|default([]) -%}
{%- set srcset = transformedImages|map(image => "#{image.getUrl()} #{image.width}w") -%}
{%- set attributes = attributes ?? {} -%}
#}


<picture>


{#
use the final crop in the transforms array
as the default
#}
{% set defaultCrop = transforms|last %}
{% set defaultCrop = defaultCrop.crop %}

{# set fallBack image for when the field is empty #}
{% set fallbackImage = 'https://placehold.co/' ~ defaultCrop.width ~ 'x' ~ defaultCrop.height ~ '/dfe4f7/bababa' %}


{% for transform in transforms %}
{# jpeg source #}
{% set crop = transform.crop %}
<source
media="{{ transform.mq }}"
srcset="{{ image.url(crop) ?? fallbackImage }}"
width="{{ transform.crop.width ?? null }}"
height="{{ transform.crop.height ?? null }}"
type="image/jpeg"
>
{# webp source #}
{% set cropWebp = transform.crop|merge({
'format': 'webp' }) %}
<source
media="{{ transform.mq }}"
srcset="{{ image.url(cropWebp) ?? fallbackImage }}"
width="{{ transform.crop.width ?? null }}"
height="{{ transform.crop.height ?? null }}"
type="image/webp"
>
{% endfor %}


<img
src="{{ image.url(defaultCrop) ?? fallbackImage }}"
alt="{{ attributes.alt }}"
class="{{ attributes.class }}"
width="{{ defaultCrop.width }}"
height="{{ defaultCrop.height }}"
loading="{{ attributes.loading }}"
{{ attributes.dataAttributes|raw }}/>
</picture>
{% set defaultCrop = transforms|last %}
{% set defaultCrop = defaultCrop.crop %}

{# set fallBack image for when the field is empty #}
{% set fallbackImage = 'https://placehold.co/' ~ defaultCrop.width ~ 'x' ~ defaultCrop.height ~ '/dfe4f7/bababa' %}


<figure class="{{ figureClass ?? null }}" style="{{ figureStyle ?? null }}">
<picture class="{{ pictureClass ?? null }}">
{% for transform in transforms %}
{# jpeg source #}
{% set crop = transform.crop %}
<source
media="{{ transform.mq }}"
srcset="{{ image.url(crop) ?? fallbackImage }}"
width="{{ transform.crop.width ?? null }}"
height="{{ transform.crop.height ?? null }}"
type="image/jpeg"
>
{# webp source #}
{% set cropWebp = transform.crop|merge({
'format': 'webp' }) %}
<source
media="{{ transform.mq }}"
srcset="{{ image.url(cropWebp) ?? fallbackImage }}"
width="{{ transform.crop.width ?? null }}"
height="{{ transform.crop.height ?? null }}"
type="image/webp"
>
{% endfor %}

{#
use the final crop in the transforms array
as the default
#}
{% set defaultCrop = transforms|last %}
{% set defaultCrop = defaultCrop.crop %}
<img
src="{{ image.url(defaultCrop) ?? fallbackImage }}"
alt="{{ attributes.alt|striptags }}"
class="{{ attributes.class }}"
width="{{ defaultCrop.width ?? null }}"
height="{{ defaultCrop.height ?? null }}"
loading="{{ attributes.loading }}"
style="{{ attributes.style ?? null }}"
{{ attributes.dataAttributes|raw }}/>
</picture>

{# image caption #}
{% if caption is defined and caption != null%}
<figcaption class="block px-4 py-2 italic border-b text-14 text-grayBrand border-text-grayBrand-light">
{{ caption }}
</figcaption>
{% endif %}
</figure>

0 comments on commit 4cfbd2b

Please sign in to comment.