Skip to content

Commit

Permalink
Added Native JS Support
Browse files Browse the repository at this point in the history
Added Native JS Support for events and rendering on elements.

Removed support for tag, children and other deprecated attributes.

Works for ES6 browsers only, removed support for legacy non ES6 browsers
  • Loading branch information
moappi committed Dec 18, 2023
1 parent 43563da commit 6df3940
Show file tree
Hide file tree
Showing 15 changed files with 583 additions and 513 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License

Copyright (c) 2022 JSON2HTML. https://www.json2html.com/
Copyright (c) 2024 JSON2HTML. https://www.json2html.com/

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
json2html
------------------

json2html is an open source javascript library that uses js templates to render JSON objects into HTML.
json2html is an open source javascript library that uses json templates to render JSON objects into HTML.

Build lightning fast, interactive client side templates using nothing but javascript.

Expand All @@ -22,11 +22,11 @@ Example
```javascript
json2html.render(
[
{"name": "Justice League", "year":2021},
{"name": "Coming 2 America", "year":2021}
{"name": "Sasha", "age":27},
{"name": "Bobby", "age":45}
],
{"<>": "li", "html":[
{"<>": "span", "text": "${name} (${year})"}
{"<>": "span", "text": "${name} (${age} years old)"}
]});

```
Expand All @@ -35,11 +35,11 @@ Will render the following html

```html
<li>
<span>Justice League (2021)</span>
<span>Sasha (27 years old)</span>
</li>
<li>
<span>Coming 2 America (2021)</span>
</li
<span>Bobby (45 years old)</span>
</li>
```

jQuery
Expand Down Expand Up @@ -72,7 +72,7 @@ Usage
```javascript
const json2html = require('node-json2html');

let html = json2html.transform([{'male':'Bob','female':'Jane'},{'male':'Rick','female':'Ann'}],{"<>":"div","html":"${male} likes ${female}"});
let html = json2html.transform([{'name':'Bob','fruit':'Bananas'},{'name':'Rick','fruit':'Apples'}],{"<>":"div","text":"${name} likes ${fruit}"});
```

TypeScript:
Expand Down
9 changes: 3 additions & 6 deletions examples/arrays.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
<head>
<title>json2html - Data Mapping &amp; Array Tutorial</title>

<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>

<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>

Expand All @@ -25,8 +22,8 @@ <h2 class="text-3xl font-bold tracking-tight text-gray-900 sm:text-4xl">Mapping
</div>

<!-- Tutorials -->
<div id="body" class="mx-auto mt-16 max-w-2xl sm:mt-20 lg:mt-24 lg:max-w-4xl border rounded p-10">
<ul class="space-y-5"></ul>
<div class="mx-auto mt-16 max-w-2xl sm:mt-20 lg:mt-24 lg:max-w-4xl border rounded p-10">
<ul id="list" class="space-y-5"></ul>
</div>

</div>
Expand Down Expand Up @@ -71,7 +68,7 @@ <h2 class="text-3xl font-bold tracking-tight text-gray-900 sm:text-4xl">Mapping

//Render the list of employees using jquery
// note we can also do this using json2html.render as we don't have any events
$("#body ul").json2html(employees,template);
document.getElementById("list").json2html(employees,template);

</script>
</html>
Expand Down
9 changes: 3 additions & 6 deletions examples/components.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
<head>
<title>json2html - Component Tutorial</title>

<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>

<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>

Expand All @@ -25,8 +22,8 @@ <h2 class="text-3xl font-bold tracking-tight text-gray-900 sm:text-4xl">Creating
</div>

<!-- Tutorials -->
<div id="body" class="mx-auto mt-16 max-w-2xl sm:mt-20 lg:mt-24 lg:max-w-4xl border rounded p-10">
<ul class="space-y-5"></ul>
<div class="mx-auto mt-16 max-w-2xl sm:mt-20 lg:mt-24 lg:max-w-4xl border rounded p-10">
<ul id="list" class="space-y-5"></ul>
</div>

</div>
Expand Down Expand Up @@ -120,7 +117,7 @@ <h2 class="text-3xl font-bold tracking-tight text-gray-900 sm:text-4xl">Creating

//Render the list of employees using jquery
// note we can also do this using json2html.render as we don't have any events
$("#body ul").json2html(employees,templates.employee);
document.getElementById("list").json2html(employees,templates.employee);

</script>
</html>
Expand Down
89 changes: 89 additions & 0 deletions examples/events.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<!DOCTYPE html>
<html>
<head>
<title>json2html - Events Tutorial</title>

<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>

<!-- json2html -->
<script src="../json2html.js"></script>

</head>
<body>

<div class="px-6 py-24 sm:px-6 sm:py-32 lg:px-8">

<!-- Header -->
<div class="mx-auto max-w-2xl text-center">
<h2 class="text-3xl font-bold tracking-tight text-gray-900 sm:text-4xl">Adding Events to your Templates</h2>
<p class="mx-auto mt-6 max-w-xl text-lg leading-8 text-gray-600">Understand how to add events to your templates like onclick and onready etc..</p>
<p class="mx-auto mt-6 max-w-xl text-base leading-8 text-gray-400">Right click and 'View page source' to see how this page was rendered.</p>
</div>

<!-- Show that we're ready -->


<!-- Tutorials -->
<div class="mx-auto mt-16 max-w-2xl sm:mt-20 lg:mt-24 lg:max-w-4xl border rounded p-10">
<span id="ready" class="inline-flex items-center rounded-md bg-green-50 px-2 py-1 text-xs font-medium text-green-700 ring-1 ring-inset ring-green-600/20 float-right">READY</span>
<ul id="list"></ul>
</div>



</div>
</div>

</body>

<script>
//In this example we'll use the same list of employees but add a button were can can remove them from the list

//Data that we want to render
const employees = [
{"name":"Jenny Brown","role":"CEO","management":true},
{"name":"Ashley White","role":"CFO","management":true},
{"name":"Brandon Green","role":"Graphic Designer"},
{"name":"Sasha Black","role":"Front End Developer"}
];

//Template we'll use to render these employees
const template = {"<>":"li","class":"group","html": [

//Span element with special logic for the class property
{"<>":"span","class":o=>{

//If the user is part of managemnt then put them in blue
if(o.management) return("text-red-600");
else return("text-pink-600");

},"text":"${name} - ${role}"},

//Add a button to remove this employee from the list
// not a great idea using an inline style, but shows that we can add any property to the DOM element
{"<>":"a","class":"ml-5 text-indigo-100 group-hover:text-indigo-800","href":"#","html":"&times;","title":"remove","onclick":function(e){
//e.event => js event
//e.obj => object we're rendering
//e.data => data object we can optional assign during the render call

//Stop the link from navigating away
// example of how can can acces the jquery event
e.event.preventDefault();

//Access this element's parent (the top level li element)
// then remove it
this.parentNode.remove();
}}

],"onready":function(e){

//Show the ready message once the dom is ready with the rendered template
// onready is an event that's unique to json2html
document.getElementById("ready").classList.remove("hidden");
}};

document.getElementById("list").json2html(employees,template);
</script>
</html>

64 changes: 29 additions & 35 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
<head>
<title>json2html - Examples</title>

<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>

<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>

Expand All @@ -25,7 +22,7 @@ <h2 class="text-3xl font-bold tracking-tight text-gray-900 sm:text-4xl">json2htm

<!-- Tutorials -->
<div id="tutorials" class="mx-auto mt-16 max-w-2xl sm:mt-20 lg:mt-24 lg:max-w-4xl">
<dl class="grid max-w-xl grid-cols-1 gap-x-8 gap-y-16 lg:max-w-4xl lg:grid-cols-3"></dl>
<dl id="tutorials-list" class="grid max-w-xl grid-cols-1 gap-x-8 gap-y-16 lg:max-w-4xl lg:grid-cols-3"></dl>
</div>

</div>
Expand All @@ -36,38 +33,35 @@ <h2 class="text-3xl font-bold tracking-tight text-gray-900 sm:text-4xl">json2htm

<script>

//Render the list of examples
$(()=>{

//Examples that we want to render
const examples = [
{"name":"Creating your First Template","url":"simple.html","desc":"Understand how to create a simple tempate that maps data using both shorthand and longhand notation along with some simple logic to toggle the class name of an element"},
{"name":"Using with jQuery","url":"jquery.html","desc":"Understand how to create templates with embedded events that can be used with jQuery"},
{"name":"Mapping Data & Working with Arrays","url":"arrays.html","desc":"Understand how to map data to your elements plus understand how to work with arrays of literals (instead of objects)"},
{"name":"Creating Re-Usable Components","url":"components.html","desc":"Understand how to create and register re-usable components"},
{"name":"Using a Wrapper Component","url":"wrapper.html","desc":"Understand how to use a component that can wrap around another template (plus why you would want to do this)"}
];

//Template we'll use to render these examples
const template = {"<>":"div","class":"flex flex-col","html":[
{"<>":"dt","class":"flex items-center gap-x-3 text-base font-semibold leading-7 text-gray-600","html":[
{"<>":"span","text":(o,index)=>{
return( (index+1) + ". " + o.name);
}}
]},
{"<>":"dd","class":"mt-4 flex flex-auto flex-col text-base leading-7 text-gray-600","html":[
{"<>":"p","class":"flex-auto","text":"${desc}"},
{"<>":"p","class":"mt-6","html":[
{"<>":"a","href":"${url}","class":"text-sm font-semibold leading-6 text-indigo-600 hover:text-indigo-800","html":[
{"<>":"span","text":"Start "},
{"<>":"span","aria-hidden":"true","html":"&rarr;"}
]}
//Examples that we want to render
const examples = [
{"name":"Creating your First Template","url":"simple.html","desc":"Understand how to create a simple tempate that maps data using both shorthand and longhand notation along with some simple logic to toggle the class name of an element"},
{"name":"Add Events","url":"events.html","desc":"Understand how to add events to your templates, like onclick etc.."},
{"name":"Mapping Data & Working with Arrays","url":"arrays.html","desc":"Understand how to map data to your elements plus understand how to work with arrays of literals (instead of objects)"},
{"name":"Creating Re-Usable Components","url":"components.html","desc":"Understand how to create and register re-usable components"},
{"name":"Using a Wrapper Component","url":"wrapper.html","desc":"Understand how to use a component that can wrap around another template (plus why you would want to do this)"},
{"name":"Using with jQuery","url":"jquery.html","desc":"Understand how to create templates with embedded events that can be used with jQuery"},
];

//Template we'll use to render these examples
const template = {"<>":"div","class":"flex flex-col","html":[
{"<>":"dt","class":"flex items-center gap-x-3 text-base font-semibold leading-7 text-gray-600","html":[
{"<>":"span","text":(o,index)=>{
return( (index+1) + ". " + o.name);
}}
]},
{"<>":"dd","class":"mt-4 flex flex-auto flex-col text-base leading-7 text-gray-600","html":[
{"<>":"p","class":"flex-auto","text":"${desc}"},
{"<>":"p","class":"mt-6","html":[
{"<>":"a","href":"${url}","class":"text-sm font-semibold leading-6 text-indigo-600 hover:text-indigo-800","html":[
{"<>":"span","text":"Start "},
{"<>":"span","aria-hidden":"true","html":"&rarr;"}
]}
]}
]};
//Render the list of example tutorials
$("#tutorials > dl").json2html(examples,template);
});
]}
]};

//Render the list of example tutorials
document.getElementById("tutorials-list").json2html(examples,template);
</script>
</html>
79 changes: 41 additions & 38 deletions examples/jquery.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,45 +37,48 @@ <h2 class="text-3xl font-bold tracking-tight text-gray-900 sm:text-4xl">Using wi
<script>
//In this example we'll use the same list of employees but add a button were can can remove them from the list

//Data that we want to render
const employees = [
{"name":"Jenny Brown","role":"CEO","management":true},
{"name":"Ashley White","role":"CFO","management":true},
{"name":"Brandon Green","role":"Graphic Designer"},
{"name":"Sasha Black","role":"Front End Developer"}
];

//Template we'll use to render these employees
const template = {"<>":"li","class":"group ","html": [

//Span element with special logic for the class property
{"<>":"span","class":o=>{

//If the user is part of managemnt then put them in blue
if(o.management) return("text-red-600");
else return("text-pink-600");

},"text":"${name} - ${role}"},

//Add a button to remove this employee from the list
// not a great idea using an inline style, but shows that we can add any property to the DOM element
{"<>":"a","class":"ml-5 text-indigo-100 group-hover:text-indigo-800","href":"#","html":"&times;","title":"remove","onclick":function(e){
//e.event => jquery event
//e.obj => object we're rendering
//e.data => data object we can optional assign during the render call

//Stop the link from navigating away
// example of how can can acces the jquery event
e.event.preventDefault();

//Access this element's parent (the top level li element)
// then remove it
$(this).parent().remove();
}}

]};
$(()=>{

$("#body ul").json2html(employees,template);
//Data that we want to render
const employees = [
{"name":"Jenny Brown","role":"CEO","management":true},
{"name":"Ashley White","role":"CFO","management":true},
{"name":"Brandon Green","role":"Graphic Designer"},
{"name":"Sasha Black","role":"Front End Developer"}
];

//Template we'll use to render these employees
const template = {"<>":"li","class":"group ","html": [

//Span element with special logic for the class property
{"<>":"span","class":o=>{

//If the user is part of managemnt then put them in blue
if(o.management) return("text-red-600");
else return("text-pink-600");

},"text":"${name} - ${role}"},

//Add a button to remove this employee from the list
// not a great idea using an inline style, but shows that we can add any property to the DOM element
{"<>":"a","class":"ml-5 text-indigo-100 group-hover:text-indigo-800","href":"#","html":"&times;","title":"remove","onclick":function(e){
//e.event => jquery event
//e.obj => object we're rendering
//e.data => data object we can optional assign during the render call

//Stop the link from navigating away
// example of how can can acces the jquery event
e.event.preventDefault();

//Access this element's parent (the top level li element)
// then remove it
$(this).parent().remove();
}}

]};

$("#body ul").json2html(employees,template);
});
</script>
</html>

Loading

0 comments on commit 6df3940

Please sign in to comment.