Retrieving optimized (hashed) image path in a template loop #4228
Replies: 6 comments 5 replies
-
|
Can you share the code for your image shortcode? |
Beta Was this translation helpful? Give feedback.
-
|
Grabbing Here is the shortcode in eleventy.js: |
Beta Was this translation helpful? Give feedback.
-
|
I'm struggling to understand how you are fitting everything together. Specifically I'm not sure what you mean by accessing the first image in the loop. I've put together a quick demo repo to show you how I would go about this. The shortcode is much simpler than yours but it should be obvious how you can adapt it to your code. In short you should be able to pass the image to the shortcode in multiple places as I've done here. It's usually about getting variable name right. See if that helps, if not if you can explain in more detail what you are actually doing (or share a repo) that would help us understand what you're trying to do. It's a different problem if you want to get the first image of a page (not one defined in front-matter) and use that for your thumbnails. I'm pretty sure people have done that before, might be worth asking on the Discord. |
Beta Was this translation helpful? Give feedback.
-
Each post has a featured image which is the first image. The frontmatter contains a key:value of And then I have: Where "featured" grabs the value of the image key in the frontmatter. In eleventy.js, the value of Because I use the image key:value in other places where the URL cannot start with Is that any clearer? I tried your code, but because of this specific restriction on the URL's format, it doesn't work ("Cannot stat /assets/images/blog/a-post-slug/image.jpg"). Thanks for the help so far. |
Beta Was this translation helpful? Give feedback.
-
|
I understand. In which case you can just append the Unless you have another good reason it's wise to pass in the URL of the image rather than rely on getting it from context ( When shortcodes start to get this complex it can sometimes be a good idea to pass in an object with your options rather than relying on a comma delimited string. Makes them much easier to read and if you set sensible defaults in the shortcode, you only have to pass in the values you need for that instance. You can still override any option you wish to. This would also allow you to pass in the So something like: {%
myImage image, {
css: "w-full h-64 object-cover",
alt: "what ever you want this to be",
srcDir: "./src"
}
%}And then in the shortcode: eleventyConfig.addShortcode("myImage", async function (src, options = {}) {
let { css,
alt,
widths = [300, 600], // can set sensible defaults
sizes = "(max-width: 600px) 100vw, 600px",
srcDir
} = options;
let sourceImagePath = `${srcDir}${src}`;
let metadata = await Image(sourceImagePath, {
widths: widths,
formats: ["jpeg", "png"],
outputDir: "dist/img/",
})
...I've updated the repo with a working version with these changes. |
Beta Was this translation helpful? Give feedback.
-
|
Final code (hopefully), based on @dwkns examples: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The index for my blog entries displays a thumbnail, link, and excerpt for each entry. I'd like to use responsive images for the thumbnails. I'm already generating the images with the Eleventy image plugin.
How can I retrieve the hashed image path inside the template loop for the thumbnails?
Here's what I currently have (simplified for clarity).
post.data.imageis a string in the individual post frontmatter.I have got an image shortcode which uses the
<picture>syntax, but I haven't found a way to use the page data inside it:Fails because no image is found at that location, likely because it's fired before the image transform.
Can anyone point me in the right direction?
Beta Was this translation helpful? Give feedback.
All reactions