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.3

Published

It converts json to html

Downloads

236

Readme

@devanshdeveloper/json-to-html

@devanshdeveloper/json-to-html is an npm package that allows you to dynamically manipulate the DOM using JSON-like data structures. It enables the creation and customization of HTML elements, applying styles, attributes, and event listeners based on JSON objects. This package can be used to generate HTML content from dynamic data and inject it directly into the DOM.

Installation

To install the package, use npm:

npm install @devanshdeveloper/json-to-html

Usage

You can use @devanshdeveloper/json-to-html to dynamically generate HTML content by providing JSON objects that describe the structure, attributes, and children of elements.

Importing

You can import the package using:

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

Example Usage

Here's a basic example of how to use @devanshdeveloper/json-to-html to dynamically generate HTML content using native DOM methods like document.querySelector.

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

const jsonToHtml = new JSONToHTML();

const dynamicData = {
  "tag": "div",
  "attributes": {
    "data": ["dish_id", "{{item.id}}"],
    "styles": {
      "display": "flex",
      "flexDirection": "column",
      "gap": "100px"
    }
  },
  "children": [
    {
      "tag": "div",
      "attributes": {
        "html": "{{item.name}}",
        "addClass" : "container",
        "styles": {
          "fontSize": "12px",
          "textAlign": "center"
        }
      }
    },
    {
      "tag": "div",
      "attributes": {
        "html": "{{item.description}}",
        "styles": {
          "fontSize": "12px",
          "textAlign": "center"
        }
      }
    },
    {
      "tag": "div",
      "attributes": {
        "styles": {
          "display": "flex",
          "justifyContent": "space-between"
        }
      },
      "children": [
        {
          "tag": "div",
          "attributes": {
            "styles": {
              "fontSize": "10px"
            },
            "html": "{{item.availability}}"
          }
        },
        {
          "tag": "div",
          "attributes": {
            "styles": {
              "fontSize": "10px"
            },
            "html": "{{item.price}}"
          }
        }
      ]
    }
  ]
};

jsonToHtml.convert(dynamicData, document.querySelector("#app"), { item: { name: "Dish", description: "A delicious dish", availability: "Available", price: "$10.00" } });

This example generates a structure with a div container that contains elements for name, description, availability, and price. It uses dynamic placeholders like {{item.name}}, which are replaced with the actual values passed in the dynamicValues parameter.

Functions

  • convert(json, appendTo, dynamicValues)

  • json: A JSON object that defines the HTML structure and attributes.

  • appendTo: The parent element to which the generated HTML will be appended (e.g., document.querySelector("#app")).

  • dynamicValues: An object containing dynamic data to replace placeholders in the JSON structure (e.g., {{item.name}}).

  • replacePlaceholders(text, dynamicValues): Replaces placeholder values inside strings with actual dynamic data.

  • manipulateDOMFromJSON(elementsArray, parentElement, dynamicValues) Generates HTML elements from a JSON array and appends them to the parent element.

JSON Structure

  • tag: The HTML tag name (e.g., "div", "span", "h1").
  • attributes: An object containing the attributes or methods to be applied to the element (e.g., "html", "styles", "data", etc.).
  • children: An optional array containing child elements, each represented by a similar JSON structure.

Example JSON

{
  "tag": "div",
  "attributes": {
    "html": "This is a sample div",
    "styles": {
      "color": "blue",
      "fontSize": "16px"
    }
  },
  "children": [
    {
      "tag": "span",
      "attributes": {
        "html": "Child element"
      }
    }
  ]
}

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! If you would like to contribute to @devanshdeveloper/json-to-html, feel free to submit a pull request or file an issue.

Contact

If you have any questions or issues, feel free to reach out via the GitHub repository or open an issue.