You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Web-Dev-For-Beginners/translations/en/7-bank-project/1-template-route
leestott 7cfaffabb5
🌐 Update translations via Co-op Translator
3 weeks ago
..
README.md 🌐 Update translations via Co-op Translator 3 weeks ago
assignment.md 🌐 Update translations via Co-op Translator 3 weeks ago

README.md

Build a Banking App Part 1: HTML Templates and Routes in a Web App

Pre-Lecture Quiz

Pre-lecture quiz

Introduction

Since JavaScript was introduced in browsers, websites have become more interactive and complex than ever. Web technologies are now often used to create fully functional applications that run directly in a browser, known as web applications. Because web apps are highly interactive, users don't want to wait for a full page reload every time they perform an action. This is why JavaScript is used to update the HTML directly via the DOM, providing a smoother user experience.

In this lesson, well lay the groundwork for creating a banking web app. Well use HTML templates to create multiple screens that can be displayed and updated without reloading the entire HTML page.

Prerequisite

Youll need a local web server to test the web app well build in this lesson. If you dont have one, you can install Node.js and use the command npx lite-server from your project folder. This will create a local web server and open your app in a browser.

Preparation

On your computer, create a folder named bank with a file named index.html inside it. Well start with this HTML boilerplate:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bank App</title>
  </head>
  <body>
    <!-- This is where you'll work -->
  </body>
</html>

HTML templates

If you want to create multiple screens for a web page, one solution is to create a separate HTML file for each screen you want to display. However, this approach has some drawbacks:

  • The entire HTML has to reload when switching screens, which can be slow.
  • Sharing data between different screens becomes difficult.

An alternative approach is to use a single HTML file and define multiple HTML templates using the <template> element. A template is a reusable block of HTML that isnt displayed by the browser until its instantiated at runtime using JavaScript.

Task

Well create a banking app with two screens: the login page and the dashboard. First, lets add a placeholder element in the HTML body that well use to display the different screens of our app:

<div id="app">Loading...</div>

Weve given it an id to make it easier to locate with JavaScript later.

Tip: Since the content of this element will be replaced, you can add a loading message or indicator that will be shown while the app is loading.

Next, lets add an HTML template for the login page below. For now, well include only a title and a section containing a link for navigation.

<template id="login">
  <h1>Bank App</h1>
  <section>
    <a href="/dashboard">Login</a>
  </section>
</template>

Then, well add another HTML template for the dashboard page. This page will include several sections:

  • A header with a title and a logout link
  • The current balance of the bank account
  • A list of transactions displayed in a table
<template id="dashboard">
  <header>
    <h1>Bank App</h1>
    <a href="/login">Logout</a>
  </header>
  <section>
    Balance: 100$
  </section>
  <section>
    <h2>Transactions</h2>
    <table>
      <thead>
        <tr>
          <th>Date</th>
          <th>Object</th>
          <th>Amount</th>
        </tr>
      </thead>
      <tbody></tbody>
    </table>
  </section>
</template>

Tip: When creating HTML templates, if you want to preview how theyll look, you can comment out the <template> and </template> lines by enclosing them with <!-- -->.

Why do you think we use id attributes on the templates? Could we use something else, like classes?

Displaying templates with JavaScript

If you try opening your current HTML file in a browser, youll see it gets stuck displaying Loading.... Thats because we need to add JavaScript code to instantiate and display the HTML templates.

Instantiating a template usually involves three steps:

  1. Retrieve the template element from the DOM, for example using document.getElementById.
  2. Clone the template element using cloneNode.
  3. Attach it to the DOM under a visible element, for example using appendChild.

Why do we need to clone the template before attaching it to the DOM? What do you think would happen if we skipped this step?

Task

Create a new file named app.js in your project folder and import it in the <head> section of your HTML:

<script src="app.js" defer></script>

Now, in app.js, well create a new function called updateRoute:

function updateRoute(templateId) {
  const template = document.getElementById(templateId);
  const view = template.content.cloneNode(true);
  const app = document.getElementById('app');
  app.innerHTML = '';
  app.appendChild(view);
}

Here, we follow the three steps described above. We instantiate the template with the id templateId and place its cloned content inside our app placeholder. Note that we use cloneNode(true) to copy the entire subtree of the template.

Now call this function with one of the templates and observe the result.

updateRoute('login');

Whats the purpose of this code app.innerHTML = '';? What happens if we dont include it?

Creating routes

In the context of a web app, Routing refers to mapping URLs to specific screens that should be displayed. On a website with multiple HTML files, this happens automatically because the file paths are reflected in the URL. For example, with these files in your project folder:

mywebsite/index.html
mywebsite/login.html
mywebsite/admin/index.html

If you create a web server with mywebsite as the root, the URL mapping will be:

https://site.com            --> mywebsite/index.html
https://site.com/login.html --> mywebsite/login.html
https://site.com/admin/     --> mywebsite/admin/index.html

However, since our web app uses a single HTML file containing all the screens, this default behavior wont work. We need to create this mapping manually and update the displayed template using JavaScript.

Task

Well use a simple object to implement a map between URL paths and our templates. Add this object at the top of your app.js file:

const routes = {
  '/login': { templateId: 'login' },
  '/dashboard': { templateId: 'dashboard' },
};

Now lets modify the updateRoute function slightly. Instead of passing the templateId directly as an argument, well retrieve it by first checking the current URL and then using our map to get the corresponding template ID. We can use window.location.pathname to get just the path section of the URL.

function updateRoute() {
  const path = window.location.pathname;
  const route = routes[path];

  const template = document.getElementById(route.templateId);
  const view = template.content.cloneNode(true);
  const app = document.getElementById('app');
  app.innerHTML = '';
  app.appendChild(view);
}

Here, we map the routes we declared to their corresponding templates. You can test this by manually changing the URL in your browser.

What happens if you enter an unknown path in the URL? How could we fix this?

Adding navigation

The next step for our app is to enable navigation between pages without manually changing the URL. This involves two things:

  1. Updating the current URL
  2. Updating the displayed template based on the new URL

Weve already handled the second part with the updateRoute function, so now we need to figure out how to update the current URL.

Well use JavaScript, specifically the history.pushState method, which allows us to update the URL and create a new entry in the browsing history without reloading the HTML.

Note: While the HTML anchor element <a href> can create hyperlinks to different URLs, it will reload the HTML by default. To prevent this behavior when handling routing with custom JavaScript, use the preventDefault() function on the click event.

Task

Lets create a new function to handle navigation in our app:

function navigate(path) {
  window.history.pushState({}, path, path);
  updateRoute();
}

This function first updates the current URL based on the given path, then updates the template. The property window.location.origin returns the root URL, allowing us to reconstruct a complete URL from a given path.

Now that we have this function, we can address the issue of what happens if a path doesnt match any defined route. Well modify the updateRoute function to add a fallback to one of the existing routes if no match is found.

function updateRoute() {
  const path = window.location.pathname;
  const route = routes[path];

  if (!route) {
    return navigate('/login');
  }

  ...

If a route isnt found, well now redirect to the login page.

Next, lets create a function to handle link clicks, retrieve the URL, and prevent the browsers default behavior:

function onLinkClick(event) {
  event.preventDefault();
  navigate(event.target.href);
}

Finally, lets complete the navigation system by adding bindings to the Login and Logout links in the HTML:

<a href="/dashboard" onclick="onLinkClick(event)">Login</a>
...
<a href="/login" onclick="onLinkClick(event)">Logout</a>

The event object captures the click event and passes it to our onLinkClick function.

Using the onclick attribute, bind the click event to JavaScript code, in this case, the navigate() function.

Try clicking these links—you should now be able to navigate between the different screens of your app.

The history.pushState method is part of the HTML5 standard and is supported by all modern browsers. If youre building a web app for older browsers, you can use a hash (#) before the path to implement routing that works with regular anchor navigation and doesnt reload the page. This was originally intended for creating internal links within a page.

Handling the browsers back and forward buttons

Using history.pushState creates new entries in the browsers navigation history. You can check this by holding the back button in your browser—it should display something like this:

Screenshot of navigation history

If you try clicking the back button a few times, youll notice that the current URL changes and the history is updated, but the same template keeps being displayed.

This happens because the app doesnt know it needs to call updateRoute() whenever the history changes. According to the history.pushState documentation, the popstate event is triggered when the state changes (i.e., when we navigate to a different URL). Well use this event to fix the issue.

Task

To ensure the displayed template updates when the browser history changes, well attach a new function that calls updateRoute(). Add this at the bottom of your app.js file:

window.onpopstate = () => updateRoute();
updateRoute();

Note: We used an arrow function here to declare our popstate event handler for brevity, but a regular function would work just as well.

Heres a refresher video on arrow functions:

Arrow Functions

🎥 Click the image above for a video about arrow functions.

Now try using the back and forward buttons in your browser. The displayed route should update correctly this time.


🚀 Challenge

Add a new template and route for a third page that shows the credits for this app.

Post-Lecture Quiz

Post-lecture quiz

Review & Self Study

Routing is one of the surprisingly tricky parts of web development, especially as the web transitions from page refresh behaviors to Single Page Application (SPA) behaviors. Read about how the Azure Static Web App service handles routing. Can you explain why some of the decisions described in that document are necessary?

Assignment

Improve the routing


Disclaimer:
This document has been translated using the AI translation service Co-op Translator. While we strive for accuracy, please note that automated translations may contain errors or inaccuracies. The original document in its native language should be regarded as the authoritative source. For critical information, professional human translation is recommended. We are not responsible for any misunderstandings or misinterpretations resulting from the use of this translation.