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.
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.
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).
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.
<!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>
Kasper-js is built with a modular architecture:
Component
class for creating reusable UI elements with their own logic and state.@if
, @each
, ``, etc.) is embedded in a way that keeps the overall HTML structure valid. This allows developers to use standard HTML tools and linters.Kasper’s template syntax is designed to be intuitive and integrate seamlessly with HTML.
<div @if="this.user.isLoggedIn">Welcome, !</div>
<div @elseif="this.isLoading">Loading...</div>
<div @else>Please log in.</div>
@each
)<ul>
<li @each="item of this.items"> (Index: )</li>
</ul>
@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
)<div @let:counter="0">
<span @while="counter < 3">
Iteration:
<!-- Use void for expressions without output -->
</span>
</div>
@on:event
)<button @on:click="this.handleClick($event)">Click Me</button>
<input @on:input="this.onInputChange($event.target.value)" />
Evaluates the expression and inserts the result as a text node.
<div>Current count: </div>
<div>Full Name: </div>
@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>
The Kasper expression interpreter supports a subset of JavaScript expressions, enabling dynamic template rendering:
variable = value
a + b
, a > b
, etc.this.myFunction(arg1, arg2)
{ key: 'value', anotherKey: this.data }
this.object.property
, this.array[0]
(a + b) * c
{ [this.dynamicKey]: 'value' }
(Note: Support level may vary)a && b
, a || b
[1, 'string', this.value]
"string"
, 123
, true
, false
, null
, undefined
new Date()
(Limited to globally accessible constructors or those imported/available in scope)this.value ?? 'default'
i++
, i--
(Primarily within loop constructs or specific contexts)this.object.property = value
`Hello, ${this.name}`
(Note: This is distinct from the `` template interpolation)condition ? valueIfTrue : valueIfFalse
typeof this.variable
!this.isTrue
, -this.value
this.myVariable
void this.doSomething()
(Used when an expression’s return value should not be rendered)Future development aims to expand the capabilities of the expression interpreter, potentially including more advanced JavaScript features and better error handling.
/spec
folder.npm test
Contributions are welcome! As a work-in-progress, there are many areas for improvement and new features. Please provide clear descriptions for your changes.
Kasper-js is licensed under the MIT License.