kasper-js

👻 Kasper-js

License: MIT Version Build Status Test Coverage

Kasper-js is a lightweight JavaScript HTML template parser and renderer. It’s designed to help developers create dynamic web UIs with a simple, intuitive syntax while offering insights into the core mechanics of modern JavaScript frameworks.

🌐 Live Demos

✨ Features

🎯 Project Vision

Kasper-js aims to bridge the gap between simple templating engines and full-fledged JavaScript frameworks by providing a lightweight, extensible solution that emphasizes performance and developer experience.

🏆 Project Goals

⚙️ Installation

  1. Include the kasper.min.js script in your HTML file:

    <script src="path/to/kasper.min.js"></script>
    

    (Or use a CDN if available in the future).

  2. Alternatively, for development, you can build the project from the source:

    npm install
    npm run build
    

    This will generate kasper.min.js in the dist folder.

📦 Usage Example

<!DOCTYPE html>
<html>
  <head>
    <title>My Kasper App</title>
    <script src="dist/kasper.min.js"></script>
    <!-- Adjust path as needed -->
  </head>
  <body>
    <template id="myAppTemplate">
      <div>Hello, !</div>
      <button @on:click="this.updateName()">Change Name</button>
    </template>

    <div id="app-root"></div>

    <script>
      class MyApp extends kasper.Component {
        appName = $state("Kasper App"); // Use $state for reactive properties

        constructor() {
          super({
            selector: "template#myAppTemplate", // Define the template to use
          });
        }

        updateName() {
          this.appName.set("Kasper App Updated!");
        }
      }

      // Initialize and render the application
      kasper.App({
        registry: {
          "my-app": MyApp,
        },
        root: "#app-root", // Specify the root element to render into
        main: "<my-app></my-app>", // Specify the main component to render
      });
    </script>
  </body>
</html>

🏗️ Architecture Overview

Kasper-js is built with a modular architecture:

💡 Design Decisions & Rationale

🛠️ Tech Stack

📝 Template Syntax Highlights

Kasper’s template syntax is designed to be intuitive and integrate seamlessly with HTML.

🔀 Conditional Rendering

<div @if="this.user.isLoggedIn">Welcome, !</div>
<div @elseif="this.isLoading">Loading...</div>
<div @else>Please log in.</div>

📋 List Rendering (@each)

<ul>
  <li @each="item of this.items"> (Index: )</li>
</ul>

🏷️ Local Variables (@let)

Evaluated during element creation, useful for aliasing or pre-calculating values.

<div
  @let="fullName = this.user.firstName + ' ' + this.user.lastName; isActive = this.user.status === 'active'"
>
  User: , Status: 
</div>

🔁 While Loops (@while)

<div @let:counter="0">
  <span @while="counter < 3">
    Iteration:  
    <!-- Use void for expressions without output -->
  </span>
</div>

🖱️ Event Handling (@on:event)

<button @on:click="this.handleClick($event)">Click Me</button>
<input @on:input="this.onInputChange($event.target.value)" />

🧩 Text Interpolation (``)

Evaluates the expression and inserts the result as a text node.

<div>Current count: </div>
<div>Full Name: </div>

🏷️ Attribute Binding (@attr:name="expression")

Dynamically sets HTML attributes.

<a @attr:href="this.url">Visit Site</a>
<img @attr:src="this.imageUrl" @attr:alt="this.imageAltText" />
<div @attr:class="this.isActive ? 'active-class' : 'inactive-class'">
  Dynamic Class
</div>

🧮 Supported JavaScript Expressions

The Kasper expression interpreter supports a subset of JavaScript expressions, enabling dynamic template rendering:

🚧 Future Enhancements for Expressions

Future development aims to expand the capabilities of the expression interpreter, potentially including more advanced JavaScript features and better error handling.

🧪 Testing

🤝 Contributing

Contributions are welcome! As a work-in-progress, there are many areas for improvement and new features. Please provide clear descriptions for your changes.

🗺️ To-Do / Future Roadmap

📄 License

Kasper-js is licensed under the MIT License.