views:

73

answers:

2

I need to generate HTML snippets using jQuery. The creation of those snippets depends on some data. The data is stored server-side, in session (where PHP is used).

At the moment I achieved this
- retrieving the data from the server via AJAX in form of JSON
- and building the snippets via specific javascript functions that read those data

The problem is that the complexity of the data is getting bigger and hence the serialization into JSON is getting even more difficult since I can't do it automatically. I can't do it automatically because some information are sensible so I generate a "stripped" version to send to the client.

I know it is difficult to understand without any code to read, but I am hoping this is a common scenario and would be glad for any tip, suggestion or even design-pattern you can give me.

Should I store both a complete and a stripped data on the server and then use some library to automatically generate the JSON from the stripped data? But this also means I have to get the two data synchronized.
Or maybe I could move the logic server-side, this way avoiding sending the data. But this means sending javascript code (since I rely on jQuery). Maybe not a good idea.

Feel free to ask me more details if this is not clear.

Thank you for any help

A: 

There are several Javascript/jQuery templating solutions available. John Resig is working on one that's likely to become a popular jQuery add-on, if not part of the core distribution. Kyle Simpson is also doing one.

I googled for a reference to it, but really I'd suggest doing your own searching because there's lots of good information out there.

edit well here's a pretty good link: http://www.west-wind.com/Weblog/posts/509108.aspx

Pointy
Thanks! I didn't think about html templates. They will really help me. Anyway the main problem of "sharing" data among client and server still remain.
Dom De Felice
A: 

You can use PHP's json_encode and json_decode methods to convert native PHP objects into JSON data representation.

Finbarr
Thank you. I will definitely use these functions instead of manually crafting the JSON like I did so far :-)
Dom De Felice