Skip to content

Add a method to the @@images view to render a img tag with srcset #170

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

Merged
merged 28 commits into from
Jun 4, 2025

Conversation

erral
Copy link
Member

@erral erral commented Mar 17, 2025

Apart from <picture> tag with several <source> tags that we already provide (according to the configuration set in the images controlpanel) images can also be rendered using a srcset attribute in the img tag.

In the srcset we can provide an arbitrary number of images, and then we provide a sizes attribute to let the browser know how much space it has. With these data, the browser decides which image from the srcset to render.

This is the approach that Volto has adopted, and I think that providing it as an option for the developers to use is really nice. The developer will just need to call the view with the size they require.

<img 
  tal:define="images context/@@images" 
  tal:replace="structure python:images.srcset('image', sizes='(min-width: 1400px) 550px, 90vw')" />

@mister-roboto
Copy link

@erral thanks for creating this Pull Request and helping to improve Plone!

TL;DR: Finish pushing changes, pass all other checks, then paste a comment:

@jenkins-plone-org please run jobs

To ensure that these changes do not break other parts of Plone, the Plone test suite matrix needs to pass, but it takes 30-60 min. Other CI checks are usually much faster and the Plone Jenkins resources are limited, so when done pushing changes and all other checks pass either start all Jenkins PR jobs yourself, or simply add the comment above in this PR to start all the jobs automatically.

Happy hacking!

@erral
Copy link
Member Author

erral commented Mar 17, 2025

@jenkins-plone-org please run jobs

@erral erral changed the base branch from master to config-with-default-template-999269c3 March 17, 2025 13:51
@erral erral force-pushed the erral-img-srcset branch from 14cb618 to 8f961e6 Compare March 17, 2025 14:21
Base automatically changed from config-with-default-template-999269c3 to master March 19, 2025 08:42
@erral erral force-pushed the erral-img-srcset branch from 8f961e6 to 26980ed Compare March 19, 2025 08:46
@erral
Copy link
Member Author

erral commented Mar 19, 2025

@jenkins-plone-org please run jobs

Copy link
Member

@mauritsvanrees mauritsvanrees left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Two small suggestions about word choice.

@MrTango Could you have a look as well please?

@erral erral force-pushed the erral-img-srcset branch from 355831f to 681facd Compare March 21, 2025 07:32
@erral
Copy link
Member Author

erral commented Mar 21, 2025

@jenkins-plone-org please run jobs

@stevepiercy
Copy link
Contributor

@plone/classicui-team could we get a review from you on this one? I stumbled across this PR and remembered recent work on the Image handling documentation with srcset.

@erral
Copy link
Member Author

erral commented Apr 3, 2025

I've made a small change. When running the srcset method, the scale is created with pre_scale which means that it only creates the scale structure and not the scaled image itself.

This means that the image will be created only when requested by the browser, which should create a better user experience when loading a page with a lot of images, not needing to create all the scales, but only the required ones.

@erral
Copy link
Member Author

erral commented May 27, 2025

I have added some documentation both here and also in a PR in the documentation project plone/documentation#1951

Copy link
Contributor

@stevepiercy stevepiercy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly rst syntax and English grammar.

erral and others added 3 commits May 27, 2025 12:13
Co-authored-by: Steve Piercy <web@stevepiercy.com>
Co-authored-by: Steve Piercy <web@stevepiercy.com>
Co-authored-by: Steve Piercy <web@stevepiercy.com>
@erral
Copy link
Member Author

erral commented May 27, 2025

Thanks @stevepiercy

@erral
Copy link
Member Author

erral commented May 27, 2025

@jenkins-plone-org please run jobs

@stevepiercy
Copy link
Contributor

@erral actually, let's kill the docs here, and put a link in the README.rst to the authoritative source at https://6.docs.plone.org/classic-ui/images.html#all-image-scales-in-the-srcset to avoid duplication and maintenance.

@erral
Copy link
Member Author

erral commented May 27, 2025

Then, I will check whether the documentation here is correctly added in the other place or not

@stevepiercy
Copy link
Contributor

Yup, let's merge plone/documentation PR right away, so it builds there.

@erral
Copy link
Member Author

erral commented May 27, 2025

@stevepiercy we shouldn't kill the whole file, because it works as a doctest of the package.

At least I will purge the "just documentation" part and leave the doctest part as is.

Copy link
Contributor

@stevepiercy stevepiercy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's get another pair of eyes for technical review. Docs LGTM!

Copy link
Member

@ale-rt ale-rt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am always confused by the code in this package and I have to lookup where the code should go.
Anyway looks like a nice improvement.
Please check if my comments make sense, I just checked the code in the browser.

@@ -739,6 +739,71 @@ def picture(
fieldname=fieldname,
).prettify()

def srcset(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the tag method having a srcset_attribute, I wonder why adding a new method rather than changing the tag one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point is that I didn't want to add a breaking change in the tag method, I want it to keep it working as it is and offer a way for the user who wants to render the srcset automatically, to get that.

I could do the changes to get that also in the tag method, but that would mean to change the semantics of it, so I discarded it.

erral and others added 2 commits May 27, 2025 16:18
@erral
Copy link
Member Author

erral commented May 27, 2025

Thanks for your review @ale-rt !

@erral
Copy link
Member Author

erral commented May 27, 2025

@jenkins-plone-org please run jobs

@erral
Copy link
Member Author

erral commented May 27, 2025

@jenkins-plone-org please run jobs

@erral erral merged commit 589f687 into main Jun 4, 2025
19 checks passed
@erral erral deleted the erral-img-srcset branch June 4, 2025 06:38
erral added a commit that referenced this pull request Jun 4, 2025
erral added a commit that referenced this pull request Jun 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

7 participants