Skip to content

Repository files navigation

Dynamo.js Documentation

A lightweight, template-driven JavaScript framework for building dynamic web applications with minimal overhead.

Overview

Dynamo.js is a simple yet powerful framework that provides:

  • Component-based architecture
  • Template rendering with variable interpolation
  • Conditional rendering and loops
  • Two-way data binding
  • AJAX interactions with declarative attributes
  • Zero dependencies

Core Concepts

1. Components

Define reusable UI components that can be included in pages.

dynamo.component('componentName', `
    <div>
        <h1>{{ title }}</h1>
        <p>{{ content }}</p>
    </div>
`);

2. Pages

Create full page layouts with data binding.

dynamo.page('#app', `
    {% include header %}
    <main>
        <h1>{{ pageTitle }}</h1>
    </main>
    {% include footer %}
`, {
    pageTitle: 'Welcome',
    // ... more data
});

3. Template Syntax

Variable Interpolation

{{ variableName }}
{{ user.name }}
{{ product.price }}

Component Inclusion

{% include componentName %}

Conditional Rendering

{% if showMessage %}
    <p>{{ message }}</p>
{% endif %}

For Loops

Simple arrays:

{% for item in items %}
    <li>{{ item }}</li>
{% endfor %}

Object arrays:

{% for city in cities %}
    <li>
        <a href="/city/{{ city.slug }}">{{ city.name }}</a>
    </li>
{% endfor %}

Interactive Features

Two-Way Data Binding

Bind input values to update other elements in real-time.

<input type="text" dy-model="username" placeholder="Enter name">
<p>Hello, <span dy-bind="username">Guest</span>!</p>
  • dy-model: Binds input value to a state variable
  • dy-bind: Updates element content when state changes

AJAX Requests

Make HTTP requests declaratively without writing fetch code.

GET Requests

<button dy-get="api/data.json" 
        dy-target="#results" 
        dy-swap="innerHTML">
    Load Data
</button>

POST Requests

<form dy-post="api/submit" 
      dy-target="#response" 
      dy-swap="innerHTML">
    <input name="email" type="email">
    <button type="submit">Submit</button>
</form>

Attributes

  • dy-get: URL for GET request
  • dy-post: URL for POST request
  • dy-target: CSS selector for target element (default: body)
  • dy-swap: How to insert content (default: innerHTML)

Swap Options

  • innerHTML: Replace target's content
  • outerHTML: Replace target element entirely
  • beforebegin: Insert before target
  • afterbegin: Insert at start of target
  • beforeend: Insert at end of target
  • afterend: Insert after target

API Reference

dynamo.component(name, template)

Register a reusable component.

Parameters:

  • name (string): Component identifier
  • template (string): HTML template with Dynamo syntax

Example:

dynamo.component('header', `
    <header>
        <h1>{{ siteName }}</h1>
        <nav>
            <a href="/">Home</a>
            <a href="/about">About</a>
        </nav>
    </header>
`);

dynamo.page(selector, template, data)

Render a page with data.

Parameters:

  • selector (string): CSS selector for target element
  • template (string): HTML template with Dynamo syntax
  • data (object): Data object for template interpolation

Example:

dynamo.page('#app', `
    {% include header %}
    <main>
        <h1>{{ title }}</h1>
        {% for item in items %}
            <p>{{ item }}</p>
        {% endfor %}
    </main>
`, {
    title: 'My Page',
    items: ['First', 'Second', 'Third']
});

dynamo.render(selector, data)

Re-render an existing element with new data.

Parameters:

  • selector (string): CSS selector for target element
  • data (object): New data object

Example:

dynamo.render('#app', {
    title: 'Updated Title',
    items: ['New', 'Items']
});

Complete Example

HTML Structure

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Dynamo App</title>
</head>
<body>
    <div id="app"></div>
    
    <script src="dynamo.js"></script>
    <script src="components.js"></script>
    <script src="pages/home.js"></script>
</body>
</html>

components.js

dynamo.component('header', `
    <header>
        <h1>{{ siteName }}</h1>
        <nav>
            <a href="/">Home</a>
            <a href="/about">About</a>
        </nav>
    </header>
`);

dynamo.component('footer', `
    <footer>
        <p>&copy; {{ year }} {{ siteName }}</p>
    </footer>
`);

pages/home.js

dynamo.page('#app', `
    {% include header %}
    
    <main>
        <h2>{{ title }}</h2>
        
        <div>
            <input type="text" dy-model="username" placeholder="Your name">
            <p>Hello, <span dy-bind="username">Guest</span>!</p>
        </div>
        
        <button dy-get="api/items.json" 
                dy-target="#items" 
                dy-swap="innerHTML">
            Load Items
        </button>
        
        <div id="items">
            <ul>
                {% for item in items %}
                    <li>{{ item }}</li>
                {% endfor %}
            </ul>
        </div>
        
        {% if showMessage %}
            <p>{{ message }}</p>
        {% endif %}
    </main>
    
    {% include footer %}
`, {
    siteName: 'My App',
    year: new Date().getFullYear(),
    title: 'Welcome',
    items: ['Item 1', 'Item 2', 'Item 3'],
    showMessage: true,
    message: 'Hello from Dynamo!'
});

Best Practices

  1. Component Organization: Keep components in a separate components.js file
  2. Page Structure: Organize pages in a pages/ directory
  3. Data Separation: Pass data objects rather than hardcoding values
  4. Nested Properties: Use dot notation for nested data (user.name, product.price)
  5. Unique Selectors: Ensure dy-target selectors are unique to avoid conflicts
  6. State Management: Use dy-model and dy-bind for simple reactive updates

Browser Support

Dynamo.js uses modern JavaScript features:

  • ES6+ syntax
  • Fetch API
  • Template literals
  • Arrow functions

Supports all modern browsers (Chrome, Firefox, Safari, Edge).

Performance Tips

  1. Minimize template complexity for faster rendering
  2. Use specific CSS selectors for better performance
  3. Avoid deeply nested loops when possible
  4. Cache component templates for reuse
  5. Use dy-swap options strategically to minimize DOM updates

Limitations

  • No virtual DOM (direct DOM manipulation)
  • Single-level nesting for object properties in loops
  • No built-in routing (use server-side routing)
  • No state management beyond simple binding
  • Templates are processed once (not reactive by default)

License

MIT License - Free to use in personal and commercial projects.

About

Dynamo.js A lightweight, template-driven JavaScript framework. Zero Dependencies, Component-Based, Reactive and Only 5KB

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages