Skip to content

Commit f1b2e6c

Browse files
author
Daniel Morse
committed
feat: fully support "ratio" prop in image Twig template
1 parent 0b739b1 commit f1b2e6c

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

packages/components/bolt-image/src/image.twig

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,20 @@
2828

2929
{% set is_jpg = src|split('.')|last == "jpg" %}
3030

31-
{# Currently 'ratio' can be string or boolean, boolean is DEPRECATED #}
32-
{% if ratio is iterable %}
33-
{% set ratio_string = ratio %}
34-
{% elseif ratio is sameas(false) or ratio is sameas(true) %}
31+
{# 'ratio' should be a string, but boolean was previously supported. Boolean is now DEPRECATED. #}
32+
{% if ratio is sameas(false) or ratio is sameas(true) %}
3533
{% set ratio_bool = ratio %}
34+
{% else %}
35+
{% set ratio_string = ratio %}
36+
{% set ratio_split = ratio_string|split("/") %}
37+
{% if ratio_split|length == 2 %}
38+
{% set width = ratio_split[0] %}
39+
{% set height = ratio_split[1] %}
40+
{% endif %}
3641
{% endif %}
3742

38-
{% set useAspectRatio = useAspectRatio or ratio_bool ?? true %}
39-
{% set can_use_ratio = width > 0 and height > 0 and useAspectRatio is sameas(true) and not cover %}
43+
{% set use_ratio = useAspectRatio or ratio_bool ?? true %}
44+
{% set can_use_ratio = use_ratio is sameas(true) and ratio_string != "none" and width > 0 and height > 0 and not cover %}
4045
{# Only JPGs allowed, PNGs can have transparency and may not look right layered over placeholder #}
4146
{% set can_use_placeholder = can_use_ratio or cover %}
4247

@@ -109,7 +114,7 @@
109114
{% if srcset %}srcset="{{ srcset }}"{% endif %}
110115
{% if alt %}alt="{{ alt }}"{% endif %}
111116
{% if sizes %}sizes="{{ sizes }}"{% endif %}
112-
{% if ratio_string %}ratio="{{ ratio_string }}"{% elseif not useAspectRatio %}ratio="none"{% elseif can_use_ratio %}ratio="{{ width }}/{{ height }}"{% endif %}
117+
{% if ratio_string %}ratio="{{ ratio_string }}"{% elseif not use_ratio %}ratio="none"{% elseif can_use_ratio %}ratio="{{ width }}/{{ height }}"{% endif %}
113118
{% if placeholder_color %}placeholder-color="{{ placeholder_color }}"{% endif %}
114119
{% if placeholder_image %}placeholder-image="{{ placeholder_image }}"{% endif %}
115120
{% if zoom %}zoom{% endif %}

0 commit comments

Comments
 (0)