Build templates in PHP or JAVASCRIPT?

Asked

Viewed 28 times

1

The question seems obvious, but I would like to understand it in more technical terms. In my application I need to place objects in an HTML template for user viewing. I have two options and I would like to know which is the most efficient.

1º - Send only objects for AJAX request and mount the HTML template in JAVASCRIPT and place it in the interface.

2º - Assemble the HTML template in PHP send for AJAX request and just put it in the interface.

I am currently using 2nd, but I think the first option is more efficient. I would like to know if I am right and the reason why.

1 answer

1


That question doesn’t have an exact answer as I said in my comment in that question:

I believe that in general the more on the client side the better, but there are cases where this can change, if your application is focused on users who do not have very powerful machines, a tool to be used with heavy applications or you are making an internal system for a sustainable company that still uses windows Xp

Making a comparison between returning only the crucial data in JSON or html format ready (data, html tags, styles, ...), the first will be clearly faster but depends on whether you want to relieve the back end or the front end

Even if you prefer to relieve the front-end, I believe that it is worth more to just send the data in json format, and in html create the table, if you need to do checks to, for example, change the background color of each line according to a specific data of that line, you can do these if else/switch case in the back-end and returns the color of that line with the json. The time lost when sending more data, in general, does not pay to leave everything in the back

  • This last paragraph you wrote is just the question point, me mounting the card in PHP and returning, each object already mounted on the card gets approximately 1315 characters and the return being only the pure object it gets much less characters, but javascript will take the trouble to structure and put in its proper place in html. From what you quoted there is no exact calculation of cost or rule that should follow, I understood right ?

  • Yes, you understand. I believe my answer was a bit confusing, but in short: relieving back or front? , the more data passed to the front the longer, is it worth leaving everything to the back? put on the scale, test both shapes and see which one is the best in your case

  • There is also the question of compatibility, your users' browsers have JS support? Is it active? Another thing that can interfere is how is your front, it is already heavy with various animation effects or with some other task?

  • It has support yes, the interface is super light. I understood. Vlw thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.