Details Pages with EJS
Details Pages
So far we have a view that list all the films (with images). Next we'll make each of these listings link to a second page that will display specific information about that individual film. We will do that by re-visiting a concept we saw with the API example where a value is sent in the URL in the format localhost:3000/filmDetails/645268083f37a8fd938b26a1
Build the links
Edit src/views/films.ejs
to include links to a route of film/:id
.
Note how the dynamic value of <%= film._id %>
is used to create individual links for each film. We also make both the filmTitle
and the images links to make the application more user friendly.
Adding the Controller
Add a controller for extracting data for one film:
Ensure you add the getDataById
method to the exported functions.
Create a route that receives the id
as a URL parameter and then passes the id
to the getDataById
method in the controller.
The Response object res
uses the render()
method to call the filmDetails
view and pass two values, title
and film
. These are then references in the EJS file:
The hyperlinks in the films view should now link through to the details view.
Challenge: Add more information to the filmDetails
view such as the film certification and release date. Can you also display the image?