EJS Lab Exercise
Introduction
In this lab, you’ll construct a dynamic website for a fictional restaurant, “The Green Byte Bistro.” This hands-on experience is designed to solidify your understanding of using EJS templates with an Express.js application.
A quick note before you dive in
Don’t hesitate to collaborate with your classmates when working through labs.
If you get stuck during the lab, we recommend revisiting the lesson materials first. They’re designed to provide you with the information and examples that will help you complete the exercises.
What You’ll Build
You’re going to create a vibrant, interactive website for The Green Byte Bistro. This website will feature:
- Homepage: Displays key details of the bistro - name, address, and contact info.
- Nav Bar: Includes links to various sections of the site.
- Menu Page: Showcases the menu, sorted into mains, desserts, and sides.
- Category Page: Generates pages for each menu type, using route parameters for content rendering.
Lab exercises
First, copy and paste the following data to be used in the lab above your / route in server.js:
const RESTAURANT = {
name: 'The Green Byte Bistro',
isOpen: true,
address: '742 Evergreen Rd, Mapleview, OS 45502',
phone: '555-321-9876',
menu: [
{
id: 1,
name: 'Quantum Quinoa Mushroom Burger',
price: 13.00,
rating: 4,
category: 'mains',
details: 'A vegetarian burger made with a quinoa and mushroom patty, it will take you to another realm.'
},
{
id: 2,
name: 'Binary Berry Cheesecake',
price: 10.11,
rating: 3,
category: 'desserts',
details: 'A creamy cheesecake bursting with flavor. A mix of berries in every byte.'
},
{
id: 3,
name: 'Recursive Rigatoni',
price: 17.00,
rating: 5,
category: 'mains',
details: 'A classic rigatoni pasta dish, layered with rich tomato sauce and herbs. You\'ll keep coming back for more.'
},
{
id: 4,
name: 'Pumpkin Pi Squared',
price: 3.14,
rating: 5,
category: 'desserts',
details: 'A delightful pumpkin dessert, squared and spiced to perfection.'
},
{
id: 5,
name: 'Fibonacci String Bean Fries',
price: 11.23,
rating: 5,
category: 'sides',
details: 'Crispy and lightly seasoned string bean fries, served in a pattern for a fun twist.'
}
]
}
Exercise 1: Create a homepage
Create a view
- Create an
home.ejsfile inside of yourviewsdirectory. Once created, add the following boilerplate:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<h1>Home Page</h1>
</body>
</html>
Modify the route
- Now modify the existing route that handles the homepage
/request. Update this route to render thehome.ejstemplate in response to a request.
👀 When you refresh your browser, it should now be rendering the
<h1>with the contentHome Page.
Send data to view
-
Using the
locals object, send theRESTAURANTdata fromserver.jsto thehome.ejsview. -
Using EJS tags, update your HTML to display the restaurant details. Include
name,address, andphone. -
Update the
<title>of yourhomepage to dynamically match the restaurant name. -
Level-Up: Include conditional rendering to add the line “Yes we are open!” to the page if the restaurant
isOpen, and “Sorry, we are closed.” otherwise.
Exercise 2: Create a nav bar
Create a Nav partial
- Create a
partialsdirectory inside youviewsdirectory. - Inside
partials, create anav.ejsfile.
Add the following code to nav.ejs:
<nav>
<a href="/">Home</a>
<a href="/menu">Full Menu</a>
</nav>
Add partial to home page
Add your partial file to the top of the <body> in home.ejs
<%- include('./partials/nav') %>
👀 Refresh to see a new nav bar appear.
Create a new route
-
Create a new
menuroute inserver.jsGETroute to/menures.render()amenu.ejsview (created next)
Create a matching view
-
Create a
menu.ejsview for this new route in theviewsdirectory. -
Add boilerplate HTML to your new view:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Menu</title>
</head>
<body>
<h1>Full Menu</h1>
</body>
</html>
👀 Test your new view in the browser. You should see “Full Menu” on the page.
- Add your
nav.ejspartial to your menu page.
Send data to view
-
Using the
locals object, pass themenuarray data fromserver.jsto themenu.ejsview. -
In
menu.ejs, Use aforEach()loop, to render each of the menu items and their details to the page. Includename,price,ratinganddetailsfor each item. -
Level Up: Using conditionals, refactor your code to render the items under three separate
<h2>headers based on the category of the dish-"Mains""Desserts"or"Sides".
Exercise 3: Create a separate page for menu categories
Create new links in the nav
- Add the following new links to
nav.ejs
<nav>
<a href="/">Home</a>
<a href="/menu">Full Menu</a>
<!-- new links below-->
<a href="/menu/mains">Mains</a>
<a href="/menu/desserts">Desserts</a>
<a href="/menu/sides">Sides</a>
</nav>
Create a new route
-
Create a new
/menu/:categoryroute inserver.jsGETroute to/menu/:categorythat uses route parameters to determine the menu category being passed to that routeres.render()acategory.ejsview (created next)
Create a matching view
-
Create a
category.ejsview for this new route in theviewsdirectory. -
Use
menu.ejsas an example and add boilerplate HTML to your new view. Make sure to add thenavpartial to the top of the page.
Send data to view
-
Using the
locals object, pass an array of data calledmenuItemscontaining only items that match thereq.paramscategory to thecategory.ejsview. Best practice dictates that this data should be filtered before it is sent to the view. This can be done in the route handler with afor loopor the.filter()method. -
Send the category
namein thelocals objectalong with the filtered menu data. Level-Up: Capitalize the first letter for a better UI experience. -
Once in the view, use a
forEach()loop, to render each of the menu items and their details to the page. Includename,price,ratinganddetailsfor each item. -
Add the name of the category to the
<title>of the page and to an<h1>.