npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@devanshdeveloper/json-to-html

v1.0.7

Published

It converts json to html

Downloads

36

Readme

@devanshdeveloper/json-to-html

@devanshdeveloper/json-to-html is an npm package that allows you to dynamically create and manipulate the DOM using JSON-like data structures. It simplifies the creation of HTML elements, applying attributes, styles, event listeners, and more—all based on JSON objects. This package is ideal for generating HTML content from dynamic data and injecting it directly into the DOM.

Installation

You can install the package via npm:

npm install @devanshdeveloper/json-to-html

Usage

@devanshdeveloper/json-to-html enables you to describe the structure, attributes, and children of elements in JSON format and convert them into HTML. You can dynamically populate and manage the DOM using this structured data.

Importing

To use the package, import it as follows:

import { JSONToHTML } from "@devanshdeveloper/json-to-html";

Example Usage

1. Basic Example: Creating Simple HTML Structure

This example demonstrates how to create a simple HTML structure with a div and child elements like h1 and p:

import { JSONToHTML } from "@devanshdeveloper/json-to-html";

const jsonToHtml = new JSONToHTML();

const productCardTemplate = {
  "{{#each products}}": {
    tag: "div",
    attributes: {
      styles: {
        width: "300px",
        border: "1px solid #ddd",
        borderRadius: "8px",
        overflow: "hidden",
        boxShadow: "0 4px 8px rgba(0,0,0,0.1)",
        transition: "0.3s",
        backgroundColor: "#fff",
        fontFamily: "Arial, sans-serif",
      },
      addClass: "product-card",
    },
    children: [
      {
        tag: "img",
        attributes: {
          attr: {
            src: "{{#var item.imageUrl}}",
            alt: "{{#var item.imageAlt}}",
          },
          styles: { width: "100%", height: "auto" },
        },
      },
      {
        tag: "div",
        attributes: { styles: { padding: "16px" } },
        children: [
          {
            tag: "h2",
            attributes: {
              html: "{{#var item.title}}",
              styles: { fontSize: "1.2em", marginBottom: "10px" },
            },
          },
          {
            tag: "p",
            attributes: {
              html: "{{#var item.description}}",
              styles: {
                fontSize: "0.9em",
                color: "#666",
                marginBottom: "10px",
              },
            },
          },
          {
            tag: "p",
            attributes: {
              html: "${{#var item.price}}",
              styles: {
                fontSize: "1.1em",
                fontWeight: "bold",
                color: "#1a1a1a",
                marginBottom: "15px",
              },
            },
          },
          {
            tag: "button",
            attributes: {
              html: "Buy Now",
              styles: {
                padding: "10px",
                backgroundColor: "#4CAF50",
                color: "#fff",
                border: "none",
                borderRadius: "4px",
                cursor: "pointer",
              },
            },
          },
        ],
      },
    ],
  },
};

const products = [
  {
    title: "Wireless Headphones",
    description: "Noise-canceling headphones",
    price: 99.99,
    imageUrl: "https://via.placeholder.com/300x200?text=Headphones",
    imageAlt: "Wireless Headphones",
  },
  {
    title: "Smartphone Stand",
    description: "Adjustable smartphone stand",
    price: 14.99,
    imageUrl: "https://via.placeholder.com/300x200?text=Smartphone+Stand",
    imageAlt: "Smartphone Stand",
  },
  // Other products here...
];

const compiledTemplate = jsonToHtml.compile(
  {
    tag: "div",
    attributes: {
      styles: { display: "flex", gap: "20px", flexWrap: "wrap" },
    },
    children: productCardTemplate,
  },
  { products }
);
jsonToHtml.convert(compiledTemplate, document.querySelector("#app"));

2. Handling Conditional Rendering

This example demonstrates conditional rendering within the JSON structure. Let's say you only want to show the "Out of Stock" message if the product is not available.

const jsonToHtml = new JSONToHTML();

const conditionalTemplate = {
  tag: "div",
  attributes: {
    addClass: "product-card",
    styles: {
      border: "1px solid #ccc",
      borderRadius: "8px",
      padding: "16px",
      marginBottom: "20px",
    },
  },
  children: [
    {
      tag: "h2",
      attributes: {
        html: "{{#var item.name}}",
        styles: {
          fontSize: "18px",
          color: "#333",
        },
      },
    },
    {
      tag: "p",
      attributes: {
        html: "{{#var item.description}}",
        styles: {
          fontSize: "14px",
          color: "#666",
        },
      },
    },
    {
      tag: "p",
      attributes: {
        html: "{{#if item.inStock 'In Stock' 'Out of Stock'}}",
        styles: {
          color: "{{#if item.inStock 'green' 'red'}}",
          fontWeight: "bold",
        },
      },
    },
  ],
};

// Single product data
const productData = {
  item: {
    name: "Gaming Laptop",
    description: "High-performance laptop",
    inStock: false,
  },
};

const compiledTemplate = jsonToHtml.compile(conditionalTemplate, productData);
jsonToHtml.convert(compiledTemplate, document.querySelector("#app"));

3. Unit Conversion Helper

You can also use the custom helper cssConvert for unit conversion within your styles:

const jsonToHtml = new JSONToHTML();

const unitConversionTemplate = {
  tag: "div",
  attributes: {
    styles: {
      height: "{{#cssConvert '150px' 'em'}}",
      width: "{{#cssConvert '300px' 'em'}}",
      backgroundColor: "lightblue",
      textAlign: "center",
      padding: "10px",
    },
    html: "This div has dynamic unit conversion",
  },
};

const compiledTemplate = jsonToHtml.compile(unitConversionTemplate, {});

jsonToHtml.convert(compiledTemplate, document.querySelector("#app"));

This example dynamically converts the height and width of the div from px to em.

Functions

convert(json: ElementData[], parentElement: HTMLElement | null)

Converts a JSON array into DOM elements and appends them to the specified parent element.

  • json: A JSON array describing the HTML structure.
  • parentElement: The parent DOM element where the generated elements will be appended.

compile(template: any, data: any)

Applies dynamic data to a template containing placeholders ({{}}) and returns a populated JSON structure.

  • template: A JSON object describing the HTML structure with placeholders.
  • data: Dynamic data to replace placeholders in the template.

License

This project is licensed under the MIT License.

Contributing

We welcome contributions! If you’d like to contribute to @devanshdeveloper/json-to-html, please submit a pull request or file an issue on the GitHub repository.