Socket
Book a DemoInstallSign in
Socket

@sprout-js/sprout

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sprout-js/sprout

A zero-dependency library for turning JSON into reactive DOM using native HTML templates

latest
Source
npmnpm
Version
0.1.0
Version published
Maintainers
1
Created
Source

🌱 Sprout

A zero-dependancy library for turning JSON into reactive DOM.

Features

  • Zero Dependencies: Pure vanilla JavaScript with no external libraries
  • Declarative Syntax: Use familiar HTML template elements with data attributes
  • Reactive Rendering: Automatically re-renders when data changes
  • Lightweight: Small footprint perfect for simple to medium complexity applications
  • Template-First: Works directly with native HTML <template> elements

Quick Start

Installation

npm install @sprout-js/sprout

Basic Usage

<!DOCTYPE html>
<html>
<head>
    <title>My Sprout App</title>
</head>
<body>
    <!-- Define your template with the 🌱 identifier -->
    <template is="🌱" data-json='{"message": "Hello, World!", "count": 42}'>
        <h1>{message}</h1>
        <p>Count: {count}</p>
    </template>

    <script type="module">
        import '@sprout-js/sprout';
    </script>
</body>
</html>

Core Concepts

Templates

Sprout uses standard HTML <template> elements marked with is="🌱". Data is provided via the data-json attribute:

<template is="🌱" data-json='{"users": [...], "title": "User List"}'>
    <!-- template content -->
</template>

Data Binding

Use curly braces {} for simple interpolation:

<h1>{title}</h1>
<p class="{cssClass}">{user.name}</p>
<span>{user.age}</span>

Loops

Iterate over arrays using data-for:

<template data-for="{user in users}">
    <div class="user-card">
        <h3>{user.name}</h3>
        <p>Age: {user.age}</p>
    </div>
</template>

The _ variable refers to the root data context:

<template is="🌱" data-json='[{"name": "Alice"}, {"name": "Bob"}]'>
    <template data-for="{person in _}">
        <p>{person.name}</p>
    </template>
</template>

Conditionals

Show or hide content based on data conditions:

<!-- Show content if condition is truthy -->
<template data-if="{user.isAdmin}">
    <div class="admin-panel">Admin Controls</div>
</template>

<!-- Show content if condition is falsy -->
<template data-unless="{user.isLoggedIn}">
    <div class="login-prompt">Please log in</div>
</template>

Complete Example

<!DOCTYPE html>
<html>
<body>
    <template is="🌱" data-json='[
        {"name": "John", "age": 30, "isAdmin": true},
        {"name": "Jane", "age": 25, "isAdmin": false}
    ]'>
        <div class="user-list">
            <template data-for="{user in _}">
                <div class="user-card">
                    <template data-if="{user.isAdmin}">
                        <h2>👑 Admin: {user.name}</h2>
                        <p class="admin-age">Age: {user.age}</p>
                    </template>

                    <template data-unless="{user.isAdmin}">
                        <h2>{user.name}</h2>
                        <p class="user-age">Age: {user.age}</p>
                    </template>
                </div>
            </template>
        </div>
    </template>

    <script type="module">
        import '@sprout-js/sprout';
    </script>
</body>
</html>

API Reference

Template Attributes

AttributeDescriptionExample
is="🌱"Marks a template as a Sprout component<template is="🌱">
data-jsonProvides data context as JSON stringdata-json='{"key": "value"}'
data-forCreates a loop over an iterabledata-for="{item in items}"
data-ifConditional rendering (truthy)data-if="{user.isActive}"
data-unlessConditional rendering (falsy)data-unless="{user.isGuest}"
data-keyProvides unique keys for loop itemsdata-key="{user.id}"

Data Context

  • _ - References the root data object
  • Dot notation for nested properties: {user.profile.name}
  • Array access and object properties work seamlessly

Reactivity

Sprout automatically watches for changes to the data-json attribute and re-renders the template when the data updates. This makes it easy to build dynamic interfaces:

// Update the template data
const template = document.querySelector('template[is="🌱"]');
const newData = {users: [...], timestamp: Date.now()};
template.setAttribute('data-json', JSON.stringify(newData));
// Template automatically re-renders!

Browser Support

  • Modern browsers with ES6+ support
  • Uses native <template> elements and MutationObserver
  • No IE support (modern web standards only)

Development

# Install dependencies
npm install

# Run tests
npm test

Philosophy

Sprout embraces the principle that templates should look like the output they generate. By leveraging native HTML template elements and minimal JavaScript enhancement, Sprout provides a gentle introduction to reactive templating without the complexity of larger frameworks.

Perfect for:

  • Small to medium interactive websites
  • Progressive enhancement of existing HTML
  • Learning reactive concepts
  • Situations where bundle size matters

License

MIT

Keywords

templating

FAQs

Package last updated on 29 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts