LamanHub Logo
Blog

Creating Fast and Dynamic Websites with Laman.js: A Simpler Approach

Tutorial September 18, 2024

Creating Websites with Laman.js

Introduction

Laman.js is a modern web framework designed to simplify the creation of dynamic, high-performance websites. In a world where web development can often be complex, Laman.js offers a solution that prioritizes simplicity without sacrificing advanced features. With Edge.js as its intuitive templating engine and Vite as its fast development tool, Laman.js allows developers to focus on building content and functionality, rather than dealing with complex configurations. This makes it an ideal choice for anyone looking to build fast, responsive, and easily manageable websites.

Preparation and Installation

Before you start with Laman.js, ensure you have the following prerequisites in place to ensure a smooth installation and development process:

Prerequisites:

  1. Node.js: Make sure you have Node.js version 18 or higher installed. Node.js is a JavaScript runtime that enables you to run JavaScript on the server side and is essential for Laman.js projects.
  2. Terminal: You will need a terminal to run Laman.js CLI (Command Line Interface) commands. Make sure you are familiar with using the terminal to manage your project.
  3. Text Editor: Choose a text editor that supports HTML syntax, such as VSCode. VSCode is highly recommended due to its many extensions that can enhance your productivity. It is advised to install the Edge.js extension in VSCode to facilitate development with Laman.js.

Installation Steps:

  1. Initialize a New Project First, you need to create a new project with Laman.js. Open your terminal and run the following command to get started:

    npm init lamanjs my-web

    This command will create a new folder named my-web and set up a basic Laman.js project structure inside it.

  2. Navigate to Project Directory After initializing the project, navigate to the project directory using:

    cd my-web

    Here, you will find the folder and files created for your project.

  3. Install Dependencies Next, you need to install all necessary dependencies for your project. Run the following command:

    npm install

    This command will download and install all required packages listed in your project's package.json file.

  4. Start Development Server With dependencies installed, it’s time to start the development server. Run:

    npm run dev

    This command will start the development server, and you can see live changes in your browser thanks to Vite's Hot Module Replacement feature.

  5. Project Structure After initializing the project, your project structure inside the my-web folder will look like this:

    my-web
    ├───public
    ├───src
    │   └───pages
    │       └───index.edge
    └───package.json

     

    • public: This folder is used to store static assets like images, fonts, and CSS files.
    • src/pages: Here you will find the index.edge file, which is the main template for your webpage.
    • package.json: This file manages your project dependencies and scripts that can be run for development and build.

Data Fetching with SSR

Laman.js allows you to integrate dynamic data into your website with Server-Side Rendering (SSR) support. Here’s a basic guide on how to perform data fetching in Laman.js:

Data Fetch Example

To display dynamic data, you can use the fetch API within your Edge.js file. Here’s a simple example of how to fetch data from an API and display it on your webpage:

  1. Update src/pages/index.edge with Data Fetching

    Modify the src/pages/index.edge file to fetch data from an API and display it. For example, to show a list of blog posts, you can use the following code:

    @let(data = await fetch('https://api.vercel.app/blog'))
    @let(posts = await data.json())
    
    <html>
      <head>
        <title>Laman.js</title>
      </head>
      <body>
        <h1>Blog Posts</h1>
        <ul>
          @each(post in posts)
            <li><a href="/posts/{{ post.id }}">{{ post.title }}</a></li>
          @end
        </ul>
      </body>
    </html>

     

    In this example:

    • fetch is used to retrieve data from an external API.
    • The retrieved data is converted to JSON using await data.json().
    • The list of blog posts is displayed within a <ul> element, with each post linking to a detailed page.

Dynamic Routing

With Laman.js, you can create dynamic routes that map URLs based on dynamic data. This allows for the creation of pages that adjust to parameters passed in the URL. Here’s how to use dynamic routing in Laman.js:

File-Based Routing

Laman.js uses a file-based routing system, where file names and directory structures within the src/pages folder determine the URLs of your pages. For example, with the following directory structure:

src
│   └───pages
│       └───index.edge
│       └───getting-started.edge
│       └───prologue.edge

 

The generated URLs will be:

  • index.edge --> /
  • getting-started.edge --> /getting-started
  • prologue.edge --> /prologue

Public and Pages Directory

In the Laman.js project structure, there are two important directories:

  1. Public Directory This directory is used for static assets such as images, fonts, and other media files, as well as compiled distribution files (dist). These files are served directly to the client without additional processing or transformation. Anything placed in the public directory will be accessible at the root of your website.
  2. Pages Directory The src/pages directory is where your routing files are located. Each file in this directory represents a route in your application. For example, a file named about.edge in the src/pages directory will correspond to the /about route in your application.

Dynamic Routes

To create routes based on dynamic data that may not be known ahead of time, you can use Dynamic Segments. Dynamic segments allow you to handle routes with parameters that are filled in at request time.

Convention: A Dynamic Segment can be created by wrapping a folder name or file name in square brackets: [folderName]. For example, [id].edge or /[slug]/index.edge.

Example of Dynamic Routing:

  1. Create Directory and File Structure Create a new directory and file for your dynamic page. For instance:

    src
    │   └───pages
    │       └───[id].edge

     

  2. Add Code for Dynamic Page Modify the [id].edge file to fetch data based on the dynamic ID:

    @let(id = req.params.id)
    @let(response = await fetch(`https://api.example.com/posts/${id}`))
    @let(post = await response.json())
    
    <html>
      <head>
        <title>{{ post.title }}</title>
      </head>
      <body>
        <h1>{{ post.title }}</h1>
        <p>{{ post.content }}</p>
      </body>
    </html>

     

    In this code:

    • req.params.id is used to retrieve the ID from the URL.
    • Data is fetched from an API using the provided ID.
    • The page displays relevant data based on the given ID.
  3. Test Dynamic Routes Run the development server and open a URL with a dynamic ID in your browser, such as /123 (where 123 is the ID you want to test). The page should display data relevant to the provided ID.

Deployment

Laman.js offers flexible deployment options to suit your needs. You can choose to deploy your app to LamanHub for a seamless experience or host it on your own server. Here are the steps to deploy to LamanHub:

Deploy to LamanHub

  1. Register on LamanHub Before deploying, you need to register on LamanHub. Visit the registration page: Register on LamanHub

    Fill out the registration form to create your account.

    LamanHub Registration Form

  2. Prepare Your Project Ensure your project is ready for deployment by following the production build steps:

    npm run build

    This command will generate the necessary production files in the ./dist directory.

  3. Deploy to LamanHub Once the build is complete, deploy to LamanHub using the following command:

    npm run deploy

    This command will automatically handle the deployment process. For the first-time deployment, the terminal will open your default web browser and prompt you to choose which project to deploy to or create a new project on the LamanHub dashboard.

    Choose LamanHub Project

    LamanHub offers:

    • Automatic Subdomain: Each project deployed on LamanHub will receive a unique subdomain.
    • SSL Certificate: LamanHub includes SSL certification for secure HTTPS connections.
    • Custom Domain: Option to configure a custom domain if needed.
  4. Verify Deployment After the deployment process is complete, visit the provided subdomain or your custom domain to ensure that your application is live and functioning as

For more detailed information on API usage, advanced features, and configuration options, please visit the official Laman.js documentation.

Conclusion

In this guide, we've explored the essentials of getting started with Laman.js, from setting up your development environment to deploying your site. By leveraging Laman.js, you benefit from a modern framework designed to streamline the development of dynamic, high-performance websites.

With its intuitive Edge.js templating engine and fast Vite-powered build process, Laman.js simplifies the creation of robust web applications. The ease of server-side rendering (SSR) data fetching and file-based routing enables you to build and manage your site efficiently while maintaining a clean, maintainable codebase. Dynamic routing further enhances your ability to handle flexible URL structures and content.

Whether you’re new to web development or an experienced developer looking for a more straightforward approach, Laman.js provides the tools and simplicity you need to build and deploy your projects effectively. By following the steps outlined in this guide, you can start building engaging websites with ease and deploy them seamlessly to LamanHub or your own server.

As you continue to develop with Laman.js, you'll find that its design principles help you focus on what truly matters—creating great web experiences for your users. Happy coding!