Reputation: 37504
Ok, I'll keep this as concise as possible (I have a tendency to waffle).
I'm making a movie related web app, so I'm making an AJAX request to the backend (PHP) of my app and it is returning me data. At the moment, I return the full JSON to my frontend (jQuery). I then .each() around the data and append the relevant parts + markup to a DIV. Now my question is, do I keep it this way (sending all data back in the request and manipulating it in the frontend) or do I loop around the data in the PHP and just send back markup to the frontend and append it to the DIV without any intervention? Which is better for optimisation? I see both options equal in maintainability.
Upvotes: 1
Views: 1652
Reputation: 1715
Rather send markup to the front-end because when you are generating a lot of html from js it will become a nightmare to maintain. Separate business logic from presentation logic (for example using smarty as tpl engine). That way your php, html and js will stay clean and maintainable as possible.
Upvotes: 0
Reputation: 2688
If all you do at the front end is whack it all into one containing element and the format doesn't change depending on the content - I'd say send markup from the back end. You can then send that 'chunk' of markup anywhere in your app and you know it'll work without extra front end coding. Leave an option to return data, incase you want to do something else with it somewhere else.
Upvotes: 0
Reputation: 1643
I spent a lot of time thinking about this for an application I was working on and I came to the decision to do generate any markup at the frontend.
The reason I went for this option is because If I ever wanted to create a mobile version of the web app I could do so with no changes to my back-end code. So in the long run it made my application far more manageable and portable to other devices etc.
Upvotes: 0
Reputation: 50029
It's better if you return just the JSON. This will serve as a nice web service and you can even switch to a full RESTful service later on. This will also be less hassle if you end up changing the markup, since you will only need to handle it on the client side. Also, if you have a clean web service going, you can add any amount of consumers (3rd party or not) and these will be able to consume your webservice since you're just returning JSON.
Upvotes: 3