Last active
June 7, 2018 07:42
-
-
Save jswhisperer/0d9bec7963806495c6bfc979a3675f22 to your computer and use it in GitHub Desktop.
dynamic images from a folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div v-for="(image, index) in images" :key="index"> | |
<img :src='image' alt="image"> | |
</div> | |
</template> | |
<script> | |
const { images } = process.env; | |
export default { | |
data() { | |
return { | |
images | |
} | |
} | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const imagesFolder = "./static/images/"; | |
const fs = require("fs"); | |
const images = []; | |
fs.readdir(imagesFolder, (err, files) => { | |
files.forEach(file => { | |
images.push(`images/${file}`); | |
}); | |
}); | |
module.exports = { | |
env: { | |
images | |
} | |
} |
Totally! @developius, you could even go further and add a description.
const [title, description] ="Title Of the Image.Some description ofimage.jpg".split(".", 2)
kind of hacky, but would just wrap in try / catch in case an image didn't follow the right naming format.
@gregbenner I don't think you'd even need a try/catch:
> const [title, description] = "Title Of the Image".split(".", 2)
> title
"title Of the image"
> description
undefined
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! Could use the filename as a caption ❤️