Transforming JSON data into an HTML template

Incorporating Angular 6 in my project, I have come up with the following templates:

  1. Header, Left panel, Body part, Footer
  2. Header, Left panel, Body part, Right panel, Footer
  3. Header, Body part, Footer

Considering the numerous templates, I am aiming to transition from hardcoding HTML parts to utilizing JSON for better organization.

The structure of the JSON file will be as follows:

{
   "horizontal": [
      {
         width: "20%",
         height: "100%"
      },
      {
         width: "80%",
         height: "100%",
         {
            "vertical": [
               {
                  width: "80%",
                  height: "60%"
               },
               {
                  width: "80%",
                  height: "40%"
               }
            ]
         }
      }
   ]
}

This JSON schema basically divides the page into a left panel (20% width) and main body section (80%), further dividing the body vertically into 60% top area and 40% bottom area.

Are there any methods available to convert this JSON structure into HTML?

Answer №1

Although this inquiry may seem overly broad, as someone who specializes in back-end development, I have navigated through numerous concepts before reaching a solution. Therefore, I have chosen to share my insights through this response.

My aim here is to demonstrate how one can create something reusable and easy to maintain with minimal code using CSS instead of converting styles from JSON to HTML. By following a small style guide, you can efficiently manage multiple templates while optimizing performance.


Instead of transferring styles from JSON to HTML, leverage CSS for its intended purpose and optimize performance by managing various templates effectively. Here are some examples:

Sample 1 (with CSS notes)

html, body, .container {
  height: 100%;
  margin: 0;
}

.container, main {
  display: flex;           
  flex-direction: column;  
}

header, footer {           

}

.wrapper {                 
  flex: 1;                
  display: flex;
}
 
aside {                   
  flex-basis: 20%;         
}

main {                     
  flex: 1;        
}

section {                  
  flex-basis: 60%;      
}

section + section {       
  flex-basis: 40%;
}


/* for demo purpose */
header, footer, aside, section {
  border: 1px dotted gray;
}
<div class="container">
  <header>Header</header>
  <div class="wrapper">
    <aside>Aside</aside>
    <main>
      <section>Section</section>
      <section>Section</section>
    </main>
  </div>
  <footer>Footer</footer>
</div>

Sample 2

html, body, .container {
  height: 100%;
  margin: 0;
}

.container, main {
  display: flex;
  flex-direction: column;
}

header, footer {
}

.wrapper {                 
  flex: 1;
  display: flex;
}
 
aside {
  flex-basis: 20%;
}

main {
  flex: 1;
}

section {
  flex-basis: 60%;
}

section + section {
  flex-basis: 40%;
}


header, footer, aside, section {
  border: 1px dotted gray;
}
<div class="container">
  <header>Header</header>
  <div class="wrapper">
    <aside>Aside</aside>
    <main>
      <section>Section</section>
      <section>Section</section>
    </main>
    <aside>Aside</aside>
  </div>
  <footer>Footer</footer>
</div>

Sample 3

html, body, .container {
  height: 100%;
  margin: 0;
}

.container, main {
  display: flex;
  flex-direction: column;
}

header, footer {
}

.wrapper {                 
  flex: 1;
  display: flex;
}
 
aside {
  flex-basis: 20%;
}

main {
  flex: 1;
}

section {
  flex-basis: 60%;
}

section + section {
  flex-basis: 40%;
}


header, footer, aside, section {
  border: 1px dotted gray;
}
<div class="container">
  <header>Header</header>
  <main>
    <section>Section</section>
    <section>Section</section>
  </main>
  <footer>Footer</footer>
</div>


For a specific template, utilize different CSS approaches (only showcasing Samples 1 and 3 here, as Sample 2 mirrors the above design):

Sample 1

html, body, .container {
  height: 100%;
  margin: 0;
}

.container, main {
  display: flex;
  flex-direction: column;
}

header, footer {
}

.wrapper {                 
  flex: 1;
  display: flex;
}
 
aside {
  flex-basis: 20%;
}

main + aside {             
  display: none;
}

main {
  flex: 1;
}

section {
  flex-basis: 60%;
}

section + section {
  flex-basis: 40%;
}


/* for demo purpose */
header, footer, aside, section {
  border: 1px dotted gray;
}
<div class="container">
  <header>Header</header>
  <div class="wrapper">
    <aside>Aside</aside>
    <main>
      <section>Section</section>
      <section>Section</section>
    </main>
    <aside>Aside</aside>
  </div>
  <footer>Footer</footer>
</div>

Sample 3

html, body, .container {
  height: 100%;
  margin: 0;
}

.container, main {
  display: flex;
  flex-direction: column;
}

header, footer {
}

.wrapper {                 
  flex: 1;
  display: flex;
}
 
aside {
  display: none;
}

main {
  flex: 1;
}

section {
  flex-basis: 60%;
}

section + section {
  flex-basis: 40%;
}


header, footer, aside, section {
  border: 1px dotted gray;
}
<div class="container">
  <header>Header</header>
  <div class="wrapper">
    <aside>Aside</aside>
    <main>
      <section>Section</section>
      <section>Section</section>
    </main>
    <aside>Aside</aside>
  </div>
  <footer>Footer</footer>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Elements that allow for asynchronous data submission without requiring a traditional submit button

Hey there, I could really use some help with this puzzle I'm trying to solve. Here's the situation: <ul> <li>Number: <span id="Number">123</span></li> </ul> I want to set up a connection between t ...

allowing absolutely positioned region to expand

For further information, please visit this page: test page In the process of designing a website, I have implemented a sidebar featuring an accordion style vertical navigation bar. The sidebar is set to be absolutely positioned in relation to its containi ...

Ways to animate elements while scrolling in React.js?

I am new to using React.JS and currently working on setting up an E-commerce Store. My goal is to have 5 images stack on top of each other and move together as the user scrolls. I used to achieve this effect in Vanilla JavaScript by adding a window.addEven ...

Trigger the callback function once the datatables DOM element has finished loading entirely

Hello there! I have a question regarding datatables. Is there any callback function available that is triggered after the datatables DOM element has finished loading? I am aware of the callbacks fnInitComplete, but they do not serve my purpose. Specificall ...

How to style a CSS list and center-align it

My website features a <ul> list with submenus structured like this: <div id="cssmenu"> <ul> <li class='active'><a href='index.html'><span>Home</span></a></li> <li class=&ap ...

Expectation in Observable: Unable to provide data retrieval

In my Angular 7 and Typescript project, I am faced with the challenge of reading a json file from within a zip file. Although I am able to successfully display the correct json output on the console, I am struggling to return this json data from the functi ...

As soon as I inserted the image, the text vanished into thin air

The phrase "welcome" is not displaying after I included the av tag <!DOCTYPE html> <html> <style> @font-face { font-family: OpenSans; src: url(OpenSans-Bold.ttf); } * { ...

Build a home page in HTML with full width design

My webpage currently allows scrolling to the right and left in HTML. I would like to change this functionality. What I envision is having my cup and book visible without the need for scrolling, with a background image centered on the page. <div cla ...

Setting predefined data in an HTML field from XML within the Odoo platform

In the model.py file of my project, I have defined an HTML field as follows: from odoo import models, fields class TestModel(models.model): _name = 'test.model' content = fields.HTML() To display the data from this model, I have used &l ...

"Utilizing Flask to efficiently manage a multitude of buttons and seamlessly update a

I am working on a web application similar to mini-tweets. The content for the posts is retrieved from a database and I want to include an 'up vote' button for each post, similar to the image shown below. https://i.sstatic.net/kJySO.png Each pos ...

The 'Bfrip' button is missing from the DOM

Having some issues with displaying table content using DataTables. Everything seems to be working smoothly except for the Buttons, which are not appearing as expected. <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.1 ...

Retrieving CSS file using admin-ajax.php in WordPress

The website has been tested on GTmetrix and you can view the report here I am puzzled as to why admin-ajax.php is causing a delay of over 3 seconds in loading time on this particular page that I am attempting to optimize. It seems to be related to a CSS ...

Developing a timetable feature using PHP

I am working on creating a schedule page that involves an SQL table named 'appointments' with columns for id, name, date, and time. My goal is to organize the data from this table into an HTML/CSS based table where all records with the same date ...

Creating a visually appealing webpage by utilizing Bootstrap and HTML for the layout

I am in the process of learning html and Bootstrap for CSS. Below is the code I currently have: <div class="container"> <div class="row text-center mb-4 mt-2"> <div class="col-md-12"> <div cla ...

Issue: StaticInjectorError(DynamicTestModule)[CityService -> Http]: Http provider not found

I am working on a service that retrieves all cities from a web service. @Injectable() export class CityService { constructor(private http: Http, private router: Router, private auth: AuthService) { } public getAllCity(): Observable<City[]> { ...

Welcome to Ng-Zorro, your destination for all things innovative and cutting

After including the NG-Zorro library in my project, every page I navigate to within my App displays only the NG-Zorro logo and the message "You have Arrived". I'm curious about removing this logo/text so that I can properly view my pages. I referred ...

Position the div at the bottom of the page

I managed to successfully position the div at the bottom using this code snippet: #div { position: absolute; width: 100%; height: 58px; margin: 0 auto; bottom: 0; left: 0; overflow: hidden; } Lately, I decided to add a sidebar ...

BeautifulSoup - Struggling to retrieve tbody element

Having trouble accessing a table nested deep inside multiple layers? I'm relatively new to utilizing Beautifulsoup and have been practicing with some basic examples. However, I've hit a roadblock as I can't seem to locate the "div" tag with ...

Is the detection change triggered when default TS/JS Data types methods are called within an HTML template?

I'm currently updating an application where developers originally included function calls directly in the HTML templating, like this: <span>{{'getX()'}}</span> This resulted in the getX method being called after each change dete ...

Which characters are permissible for the id attribute to prevent the jQuery selector from throwing an exception?

I am facing a situation where the id attribute is inputted by an end user. For instance, if the id for a textbox is "11_=11" as entered by the user, then the HTML code will appear like this: <input type="text" id="11_=11"> The corresponding jQuery ...