Chapter 3 - Create an image slideshow with Splide¶
In this chapter, you will set up Splide to display a slideshow that supports images.
Steps¶
Serve static files¶
Splide can be downloaded as separated JavaScript and CSS files that your application will host.
Create the public
directory at the root level of your working directory that NestJS will use to serve those files.
In a terminal, execute the following command(s). | |
---|---|
Note
"Serving [static] files" is a common term to illustrate the hosting and distribution of files to others. In this case, your application will serve (= distribute) the files required by Splide so when people use your application, it will download your own copy of Splide. It is called "static" because it doesn't change often.
Update the main NestJS file to serve the files from the public
directory.
- NestJS will serve files from this directory that is one level upper than the current
src
directory.
Download Splide¶
Splide is available on GitHub. Download the Git repository as a .zip
file with the following link: https://github.com/Splidejs/splide/archive/refs/heads/master.zip.
At the time of writing this tutorial, Splide is at version 4.1.3
. Yours might be more recent but it should work nonetheless.
Open the archive. From the archive, copy the following files to their destinations in your working directory:
dist/css/themes/splide-default.min.css
topublic/css/themes/splide-default.min.css
dist/css/splide.min.css
topublic/css/splide.min.css
dist/js/splide.min.js
topublic/js/splide.min.js
Your working directory should look like this.
- Sometimes the
public
directory is calledstatic
.
Validate the files are correctly served¶
Start the application. NestJS will serve the public
directory. This directory is automatically mapped to the root of you application (/
). Is means all files in the public
directory can be accessed on http://localhost:3000/path/to/my/file/in/the/public/directory
.
Try to access to the three files you added to the public
directory.
- http://localhost:3000/css/themes/splide-default.min.css
- http://localhost:3000/css/splide.min.css
- http://localhost:3000/js/splide.min.js
All these links should display a lot of text. You can safely close the tabs once all links are validated. If you have a Error 404 - Not Found
message, the files aren't at the right place.
Create a new Handlebar template for Splide¶
Now that Splide files are accessible, you can use them in a new template.
Create a new Handlebars template named views/slideshow.hbs
. This template will have a four slides slideshow.
- This file is served by our application.
- This file is served by our application.
- This file is served by our application.
- This section defines the content slideshow.
- This script initialize the slideshow that has the id
slideshow
.
Update the controller¶
Update the controller to use your new Handlebar template.
src/app.controller.ts | |
---|---|
- As the template doesn't use any variables, the function can be empty.
Check the results¶
Access http://localhost:3000. You should see a slideshow of three random images. If you refresh, the images should change as well.
To stop your NestJS application, press Ctrl+C in your terminal.
Summary¶
Congrats! You have a NestJS application that displays a slideshow with random images! At the moment, the page isn't pretty as images can have several ratios but you are now able to create slideshows with images support.
Go further¶
Are you able to add your own image to the slideshow? Expand the next component to see the answer!
Show me the answer!
Add your image to the public/images
directory. It is recommended to be of format .png
, .jpg
or .jpeg
.
Add a list element to the Splide slideshow with your file.
views/index.hbs | |
---|---|
Save your changes. If you didn't stop your NestJS application, it should automatically restart the application. Access http://localhost:3000, refresh the page and you should see the expected result.