Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1056867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:46:44+00:00 2026-05-16T17:46:44+00:00

I wonder if there are anything like a Javascript template system that wraps HTML,

  • 0

I wonder if there are anything like a Javascript template system that wraps HTML, so that we don’t have to deal with HTML directly (yeah, i know it’s a bad idea, but just out of curiosity).

So instead of writing HTML:

<body>
  <div id="title">Great work!</div>
  <span>My name is Peter</span>
</body>

We write in Json:

body: [
  {div: [
    {id: "title"},
    {body: "Great work!"}
  ]
  {span: [
    {body: "My name is Peter"}
  ]
]

I know it looks kinda weird, but I really love the thing that everything is an object 🙂

Is there a such implementation for any language? (Im using Ruby myself).

EDIT: Found something interesting:

http://jsonml.org/

Look at their examples! Brilliant!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:46:45+00:00Added an answer on May 16, 2026 at 5:46 pm

    I’ve just wrote a little example of the parser, similar to mentioned by you, using plain old JavaScript. My code is a bit dirty (as mentioned by Casey Hope, you shouldn’t extend Object.prototype) , perhaps, but it works and very easy to understand, I hope.

    The function itself:

    Object.prototype.toHtml = function(options)
    {
        //Iterates over elements
        var processElements = function(obj, handler)
        {
            //Stores found elements
            var elements = [];
    
            for (var elem in obj)
            {
                //Skips all 'derived' properties
                if (!obj.hasOwnProperty(elem)) continue;
    
                //Attribute
                if (elem.indexOf("_") == 0)
                {
                    elements.push({type: "attribute", name : /^_([a-z][0-9a-z]+)$/i(elem)[1], value : obj[elem]});
                }
                //Internal contents
                else if (elem == "contents")
                {
                    elements.push({type: "contents", value : obj[elem]});
                }
                //Text node
                else if (elem == "text")
                {
                    elements.push({type: "text", value : obj[elem]});
                }
                //Ordinary element
                else
                {
                    elements.push({type: "element", name : elem,  value : obj[elem]});
                }
            }
    
            //Returns parsed elements
            return elements;
        }
    
        //Internal function to deal with elements
        var toHtmlInternal = function(name, elements)
        {
            //Creates a new element by name using DOM
            var element = document.createElement(name);
    
            //Element children and attributes
            var children = processElements(elements);
    
            for (var i = 0; i < children.length; i++)
            {
                switch (children[i]["type"])
                {
                    case "element":
                        element.appendChild(toHtmlInternal(children[i]["name"], children[i]["value"]));
                        break;
                    case "attribute":
                        element.setAttribute(children[i]["name"], children[i]["value"]);
                        break;
                    case "text":
                        element.appendChild(document.createTextNode(children[i]["value"]));
                        break;
                    case "contents":
                        for (var j = 0; j < children[i]["value"].length; j++)
                        {
                            var content = children[i]["value"][j];
                            if (typeof content == "string")
                            {
                                element.appendChild(document.createTextNode(content));
                            }
                            else if (typeof content == "object")
                            {
                                element.appendChild(content.toHtml().firstChild);
                            }
                        }
                        break;
                }
            }
    
            //Returns it
            return element;
        }
    
        //Initial element checkment
        var initial = processElements(this);
        //Generic wrapper
        var wrapper = document.createElement("div");
    
        for (var i = 0; i < initial.length; i++)
        {
            if (initial[i]["type"] == "element")
            {
                wrapper.appendChild(toHtmlInternal(initial[i]["name"], initial[i]["value"]));
            }
        }
    
        //Returns wrapper
        return wrapper;
    };
    

    How to use:

    //A simple testing template
    var html = ({
        //Element name is just a plain name here
        body: {
    
          //Nested element
          div : {
            //All attributes are prepended with underscore
            _id : "title",
            //Content of the element is represented by such construction
            text : "Great work!"
          },
    
          span : {
            text : "My name is Peter"
          },
    
          h1 : {
            _class : "common",
            //Elements can be defined using 'contents' notation also, so we could include text nodes
            contents : ["This is my ", {a : {text: "beautiful"}} , " header"]
          }
    
        }
    }).toHtml();
    
    alert(html.innerHTML); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 545k
  • Answers 545k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would advise against using the backspace key, since that… May 17, 2026 at 9:02 am
  • Editorial Team
    Editorial Team added an answer (Note: I'll assume that by "decode and dispatch" you mean… May 17, 2026 at 9:02 am
  • Editorial Team
    Editorial Team added an answer Non static properties (alias fields, alias member variables) have their… May 17, 2026 at 9:01 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I find print_r in PHP extremely useful, but wonder if there is anything remotely
I wonder if there's a way to do the following: I have a structure
I wonder if there is something like a standalone version of Visual Studio's "Immediate
I wonder if there is a way to use ungreedy matching in JavaScript? I
I wonder if there is a way to either programatically or using a third
I wonder if there is a way to create a Custom List in Sharepoint,
i wonder if there is a way to access a control's templatepart from within
I wonder if there is a way to set the value of #define in
I wonder if there is a less verbose way to do Input Verification in
i wonder if there is a way to generate valid GUIDs/UUIDs where the first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.