Jump to content

good practices when using ajax


skaterdav85

Recommended Posts

Typically when I've used ajax I've called some server-side script that outputs some data and HTML. Would it be better practice to output the data into json or xml format, and then parse that using JS and use JS to create the HTML, thus separating your presentation layer from your back-end logic?

Link to comment
Share on other sites

It's often convenient to move data using json, but it can also be convenient to output a certain structure. If PHP outputs the structure then it's possible, for example, to change the layout or presentation using a CMS without changing the code. Of course, there are also ways to output both the data and a template of the structure, and then have Javascript put it together.

Link to comment
Share on other sites

Typically when I've used ajax I've called some server-side script that outputs some data and HTML. Would it be better practice to output the data into json or xml format, and then parse that using JS and use JS to create the HTML, thus separating your presentation layer from your back-end logic?
I'm new to jQuery, but not to AJAX. :)I usually send back a blob of HTML (sometimes formed from templates) unless there's a really good reason to send back the raw data and have the JS assemble it. Building the HTML blob from templates lets me keep the logic and presentation separate. I usually want the browser to do the least amount of work possible, and that means just displaying HTML whenever possible.I've found that using JS to build the page often slows things down significantly for anything except simple layouts or blocks. Having JS parse out data and then build a table (for example) can be awfully slow depending on the size of the table and the amount of data that needs to be parsed. Just my 2 cents. YMMV. :)
Link to comment
Share on other sites

I send back JSON in most cases, and allow the client to handle the presentation. I also use what I call 'half-JAX' where each page is a separate XHTML document (assembled from a hierarchical template architecture). Each page has its own AJAX processing. The server-side usually has a dual mode, it can deliver the full page XHTML or a JSON response.For large blocks of content, like search results, terms of use or privacy, I deliver the XHTML, but the styling remains on the client.

Link to comment
Share on other sites

Typically I just send back to the client a blob of html with the dynamic data by echoing out the html in my php script. what do you mean by templates?
A template is a blob of HTML without any code or content in it, instead it contains placeholders that the data is swapped into before it's displayed. For example this might be a "user info" template:<table><tr><td>{name_label}</td><td>{user_name}</td></tr><tr><td>{email_label}</td><td>{user_email}</td></tr></table>The {name_label} placeholder would be replaced at run time by some text that might say, "Your Name Is:".The {user_name} placeholder would be replaced at run time by some text that might say, "John Smith:".The {email_label} placeholder would be replaced at run time by some text that might say, "Your Email Is:".The {user_email} placeholder would be replaced at run time by some text that might say, "myemail@somedomain.com:".After all the replacing was done, the HTML blob could be sent to the browser or combined into a larger template.The bracketed {placeholders} aren't variables, they're just markers that a server-side rendering engine will search for and then replace with a matching bit of content (usually derived from a variable). The table above could be replaced with any kind of structure (a different looking table, CSS divs, spans, whatever) and as long as it had the right placeholders it would display the correct data when sent to the browser. A true templating system allows you to completely separate the back end code and the presentation.Smarty is a popular template engine that I personally think is awful. It uses it's own corrupt mini-version of PHP and encourages contamination of the templates with code (which is exactly what it was developed to avoid, oddly enough).RainTPL is a much simpler, lighter weight template engine that a lot of people like.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...