Skip to content

Instantly share code, notes, and snippets.

@gregpalaci
Last active June 7, 2018 07:42
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregpalaci/0d9bec7963806495c6bfc979a3675f22 to your computer and use it in GitHub Desktop.
Save gregpalaci/0d9bec7963806495c6bfc979a3675f22 to your computer and use it in GitHub Desktop.
dynamic images from a folder
<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>
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
}
}
@developius
Copy link

Nice! Could use the filename as a caption ❤️

@gregpalaci
Copy link
Author

gregpalaci commented Jun 6, 2018

Totally! @developius, you could even go further and add a description.
const [title, description] ="Title Of the Image.Some description ofimage.jpg".split(".", 2)

@gregpalaci
Copy link
Author

kind of hacky, but would just wrap in try / catch in case an image didn't follow the right naming format.

@developius
Copy link

@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