Así que soy nuevo con PUG y la programación. Quiero obtener la fuente de img e imprimirla según el nombre de la variable. Lo que quiero hacer es:
- var values = ['car', 'space', 'plants', 'rock', 'chair', 'phone', 'television'];Luego haré un foro para iterar sobre esta matriz e imprimir un despliegue aleatorio dependiendo de la palabra, algo como:
each val in values.length ? values : ['There are no values'] img(src="https://source.unsplash.com/featured/1200x900?"+val alt=val loading="lazy")He intentado diferentes formas, pero parece que no puedo saber cómo llamar e imprimir imágenes usando PUG. Intenté llamarlo usando:
img(src="https://source.unsplash.com/featured/1200x900?values[0]", alt="random")Pero solo funciona si solo digo:
img(src="https://source.unsplash.com/featured/1200x900?car", alt="random")Su código Pug:
- var values = ['car', 'space', 'plants', 'rock', 'chair', 'phone', 'television']; each val in values.length ? values : ['There are no values'] img(src="https://source.unsplash.com/featured/1200x900?"+val alt=val loading="lazy")generar HTML:
<img src="https://source.unsplash.com/featured/1200x900?car" alt="car" loading="lazy"/> <img src="https://source.unsplash.com/featured/1200x900?space" alt="space" loading="lazy"/> <img src="https://source.unsplash.com/featured/1200x900?plants" alt="plants" loading="lazy"/> <img src="https://source.unsplash.com/featured/1200x900?rock" alt="rock" loading="lazy"/> <img src="https://source.unsplash.com/featured/1200x900?chair" alt="chair" loading="lazy"/> <img src="https://source.unsplash.com/featured/1200x900?phone" alt="phone" loading="lazy"/> <img src="https://source.unsplash.com/featured/1200x900?television" alt="television" loading="lazy"/>¿Qué esperas? ¿Qué está mal?
Puede autocomprobar su código Pug en cualquier convertidor en línea, por ejemplo, aquí: https://www.ubercompute.com/pug-to-html