Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML template + JSON vs Server HTML

What do you think is better?

Use for Ajax result:

  1. HTML that was generated on server
  2. Return Data that would be used within template?

I think plus for server rendering are escaping, easy more complex logic, when much data needed!

like image 690
Igor Golodnitsky Avatar asked Nov 21 '09 15:11

Igor Golodnitsky


People also ask

What is a JSON template?

JSON templates are data files that store a list of sections to be rendered, and their associated settings. Merchants can add, remove, and reorder these sections using the theme editor.

What is HTML or JSON?

HTML is a relatively simple language, and it is easy to learn for beginners. However, it does have some limitations in terms of what kinds of things you can do with it. JSON (JavaScript Object Notation) is a language that is commonly used for data storage and transfer.

What is a templated HTML file?

The <template> tag is used as a container to hold some HTML content hidden from the user when the page loads. The content inside <template> can be rendered later with a JavaScript.


2 Answers

Both approaches have pros and cons. Returning JSON or XML from the server and using javascript templating to convert to HTML is more RESTful and has the advantage of separating data and presentation and allowing multiple clients to easily use it. The cons is that it is more work to do in javascript.

On the other hand if the server returns HTML all you have to do is inject it somewhere into the DOM. Unfortunately in this case markup and data are mixed and it would be more difficult for other clients to extract data without formating (imagine for example a desktop or mobile application that wants to consume services from your site).

IMHO if the only consumer is your site then returning HTML would be the better approach.

like image 169
Darin Dimitrov Avatar answered Oct 26 '22 20:10

Darin Dimitrov


One advantage I see with 'processing json to create markup' on client side is decrease in size of the data being transferred.

The answer to your question would depend on what kind of application you are developing. Say if you have an application where you display a list of (constantly updating) status messages on a page; sending the html would be heavier as it would contain all markup for laying out the status messages. Instead, a json object would be light enough and can be processed easily on client side into required markup.

like image 40
vsr Avatar answered Oct 26 '22 20:10

vsr