4 ms·
I don't understand how one is supposed to work with only a JSON back-end and rendering all the view with JavaScript. It's ok if it's only going into a list but
by Kiro 14y ago
I don't understand how one is supposed to work with only a JSON back-end and rendering all the view with JavaScript. It's ok if it's only going into a list but what if you want to render the data in a complicated div structure? If you render it server-side it's very easy but with JS it becomes a mess, especially if there are lot of css classes that need to be added as well.
- udfalkso 14y agoThe basic idea is that you have html templates and that you use javascript to "fill in the blanks". Here's a short primer: http://www.gethifi.com/blog/javascript-templates-and-how-we-use-them-in-hifi http://www.gethifi.com/blog/javascript-templates-and-how-we-... There are various libraries you can use to make this easier for yourself. Underscore and handlebars are too good options.
- mistercow 14y agoeco is also nice if you're using CoffeeScript, although it has a few annoying bugs and hasn't been updated in almost a year. Edit: JS -> CoffeeScript.
- orthecreedence 14y agoI've been doing this for over a year now, and honestly, it's almost exactly the same on the front-end as it is in the back-end. You have templates (with variables, loop structures, etc)and you pass data into them and render. Same as back-end. With a decent front-end framework (backbone, spine, etc) the process can be simplified even more (like using a back-end framework). I love this method because you can provide really snappy front-end experiences. It's also nice to focus on having a good solid API along with a consistent front-end, whereas splitting up your front-end between the server and the client can be more complicated than just doing it all in the client. Browsers are now capable to run the entire front-end, so why not let them?
- Kiro 14y agoThanks. What template engine are you using?
- orthecreedence 14y agoA modified version of John Resig's templating engine: http://blog.killtheradio.net/technology/a-small-modification-to-john-resigs-js-micro-templating-to-make-it-like-php/ http://blog.killtheradio.net/technology/a-small-modification...
- brokenparser 14y agoImplementing PHP syntax in JS is just ass-backwards, IMHO. Why not use something simple like Mustache? I think it's a nice middle ground because it lacks logic and runs in nearly any language. It's also used by the easiest JS templating library ever, ICanHaz.js.
- orthecreedence 14y agoObjection noted! Aside from the PHP opening/closing tags, the templating system is the same as John Reigs and is fast, simple, and works extremely well...so we haven't had a need to change. We've evaluated other options and realized that switching won't really get us anything we don't already have. Maybe once our team grows we'll re-evaluate again, since it's just two devs at the moment.