#How to concatenate a variable and a string within fences?
1 messages · Page 1 of 1 (latest)
I think you need to be using backticks for string literal interpolation:
const { size} = Astro.props;
import image from `~/assets/images/placeholders/${size}.png}`;
But also, if you import image this way, you should set the src like this
<img class="w-full" src={image}>
image.src would be undefined, because image is the string (path) you defined in your codefence.
there is also an additional } on the right that I think needs to be removed(?)
const { size} = Astro.props;
import image from `~/assets/images/placeholders/${size}.png`;
except, of course, for if the image extension is .png}
Sorry -- answering too rapidly 😄
Dynamic importing is not legal syntax in ES6 or Typescript, because dependencies are always supposed to be statically resolvable. So your editor will still complain that it expects a string literal.
I realised, I can just use
{`http://via.placeholder.com/${size}`}
but now this isn't working..
---
import Picture from '~/components/core/Picture.astro';
const {size} = Astro.props;
---
<Picture
alt="placeholder"
width={size}
height={size}
sizes="(max-width: 600px) 100vw, 600px"
src={`http://via.placeholder.com/${size}`} />```
`
If you use a remote source for your image, you need to also define the aspectRatio: https://docs.astro.build/en/guides/images/#remote-images-1
(I guess that's maybe not the case if you provide height yourself though...?)
You could also you dynamic imports if you don't want to use the Picture component
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import
Nope, didn't work
---
import Picture from '~/components/core/Picture.astro';
const {size} = Astro.props;
---
<Picture
alt="placeholder"
width={size}
aspectRatio={16/9}
sizes="(max-width: 600px) 100vw, 600px"
src={`http://via.placeholder.com/${size}`} />
What isn't working -- do you get any errors in your terminal?