#How to concatenate a variable and a string within fences?

1 messages · Page 1 of 1 (latest)

spark cloak
#

I'm trying to pass in an image size as a prop and generate a placeholder image but my code isn't working.. What am I doing wrong?

`---
const { size} = Astro.props;
import image from "~/assets/images/placeholders/${size}.png}";

<img class="w-full" src={image.src}>`

quartz flume
#

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.

split atlas
#
const { size} = Astro.props;
import image from `~/assets/images/placeholders/${size}.png`;
#

except, of course, for if the image extension is .png}

quartz flume
#

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.

spark cloak
#

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}`} />```
#

`

quartz flume
stark scaffold
spark cloak
quartz flume
#

What isn't working -- do you get any errors in your terminal?