Node.js: Code Outside the Browser / Building a Basic Server-Side Application (Without Framework)

HTML Templating: Building the Templates

๐ŸŽฏ Goal of This Note

Create the HTML templates that will later be filled in with dynamic data from the JSON file โ€” one template for the product detail page, and one for the overview page.


๐Ÿ—๏ธ Project Overview

  • The website will be dynamic: it reads product data from the data.json file.

  • If the JSON file changes (e.g., a product is removed or renamed), the website should automatically reflect that change when reloaded.

  • Two main pages will be built:

    1. ๐Ÿ  Overview page โ€” lists all products.

    2. ๐Ÿ“ฆ Product detail page โ€” shows details for a single product.


๐ŸŽจ Why Everything Is Internal (CSS & Images)

  • All CSS styling is written directly inside the HTML files (inline <style>), instead of linking external stylesheet files.

  • Instead of using real images, emojis are used to represent products.

๐Ÿง Why?

  • Every separate file (CSS file, image file, etc.) triggers its own HTTP request to the server (just like the earlier example with favicon.ico).

  • To avoid handling many extra requests for assets, everything is kept self-contained within the HTML file itself.


๐Ÿ› ๏ธ Step 1: Building the Product Detail Template

๐Ÿ“ Starting Point

  • Based on: templates/product.html (a static sample page)

This file already contains the HTML and CSS needed to get us up and running. Right now it's filled with dummy data, which we'll swap out for live data from the JSON file. Since this isn't an HTML/CSS course, we won't be going over how that markup and styling works.

๐Ÿงฎ Data Fields Available (from JSON)

Each product in data.json has fields such as:

  • ๐Ÿท๏ธ productName

  • ๐Ÿ˜€ image

  • ๐ŸŒ from

  • ๐Ÿฅ— nutrients

  • โš–๏ธ quantity

  • ๐Ÿ’ฒ price

  • ๐ŸŒฑ organic (true/false)

  • ๐Ÿ“ description

๐Ÿ”„ Swapping Static Content for Placeholders

For any data that should be dynamic, delete the static text and replace it with a placeholder like %PRODUCTNAME%.

  • ๐Ÿท๏ธ The %...% format is used because it's unlikely to clash with other text on the page.

  • ๐Ÿ’ก Any format works, as long as it's distinct and easy to spot.

Dummy Product Name

Placeholder

๐Ÿ“‹ Placeholders Used in product.html

Now let's replace all the static data that needs to be dynamic.

  • The price in the JSON is just a number (e.g., 6.50) โ€” the currency symbol (โ‚ฌ) stays hardcoded in the HTML template since it doesn't change.

๐ŸŒฑ Special Case: Organic Badge

  • ๐ŸŒฟ Products are marked organic: true or false in the JSON.

  • ๐Ÿ‘€ A CSS class hides the "organic" badge when a product isn't organic.

  • ๐Ÿท๏ธ To handle this dynamically, add a placeholder inside the class attribute:
    class="... %NOT_ORGANIC%"

  • ๐Ÿ”„ Later, this gets replaced, covered in an upcoming lecture.

๐Ÿ’พ Final Step

  • ๐Ÿ“ Save this completed file as the product template (referred to as template-product).


๐Ÿ—๏ธ Step 2: Building the Overview Template

๐ŸŽฏ The Challenge

  • ๐Ÿ“ฆ Unlike the detail page, the overview page needs to show one "card" per product โ€” but the number of products can vary (could be 2, 5, 10, etc.).

  • ๐Ÿšซ We can't hardcode a fixed number of cards in the template.

๐Ÿ’ก The Solution: Two Separate Templates

  1. overview.html currently has dummy/hardcoded code showing 5 fixed cards. This needs to be made dynamic โ€” the cards should be generated from JSON data instead of being hardcoded.

  1. Each <figure></figure> represent a product card

  1. Lets change overview to template-overview

  1. Create a new file template-card โ€” a template for just one single product card, which will be reused/repeated for every product.

  1. Now let's copy the <figure> section from template-overview.html and paste it into template-card.html

  1. Now lets clean template-overview and add a placeholder : %PRODUCT_CARDS%

  1. Later in the code, one "card" will be generated for each product in the JSON, all combined into one string, and inserted here.


๐Ÿ› ๏ธ Step 3: Modifying template-card

  • Now lets replace static data with placeholder here

  • Add %NOT_ORGANIC% as classname in %QUANITY% parent <div> which will be used for styling later

๐Ÿ”— Building the Product Link (Detail Page Navigation)

Each card contains a link (<a> element) to that product's detail page. Example real link format:

/product?id=0

๐Ÿ‘ทโ€โ™‚๏ธ Demo from the final project

  • The id in the URL matches the product's id field in the JSON
    (e.g., avocado = id: 0, goat cheese = id: 1, broccoli = id: 2, etc.).

  • In the template, this becomes: /product?id=%ID%

  • Later, %ID% will be replaced with the actual product ID for each card.

๐Ÿ’ก Note: This routing logic (handling /product?id=X requests) is not yet implemented โ€” that will be covered in an upcoming lecture.

โœ… Updated placeholder in template-card.html

๐Ÿท๏ธ Field

๐Ÿ“ Placeholder Example

Image (appears twice in the card)

%IMAGE%

Product name

%PRODUCTNAME%

Organic badge

%NOT_ORGANIC% (same concept as before)

Quantity

%QUANTITY%

Price

%PRICE%

Link

/product?id=%ID%


๐Ÿ—‚๏ธ Summary of Templates Created

๐Ÿ“„ Template Name

๐ŸŽฏ Purpose

template-product

Full detail page for a single product

template-overview

Overall overview page layout, with one placeholder (%PRODUCTCARDS%) for all product cards

template-card

A single reusable "card" โ€” repeated once per product to build the full list on the overview page