Building HTML with templates versus building it in javascript? Building HTML with templates versus building it in javascript? ajax ajax

Building HTML with templates versus building it in javascript?


Please, don't use Javascript to layout your pages. Only use it where it is needed.

Despite recent enhancements and standardizations, Javascript is still typically slow, difficult to be completely compatible (think old IE and others), and not so compatible with some screen readers and scrapers/search engines.

If you can do it by returning straight-up HTML, then that is typically the way you want to go.


There's a slightly related question here.

You could possibly use the same templates for both - this library looks intersting.

You could render templates only on the server... the question on top has a way to do it efficiently.

You could duplicate certain templates in javascript if necessary - try jqtemplate.

Personally I would probably render HTML only on the server side, to avoid template duplication. I would do JS templates if my server acted as more of a JSON web service, possibly also serving other clients.

That said, if your site being accessible and non-js friendly is important to you (it should be) then write your site fully without js, and then progressively enhance it.


Never base the functionality of your website on javascript. If you do, you will lose all visitors with javascript off. If you really want to, you need to either detect users with javascript off, and redirect them; or provide <noscript> alternative. Furthermore although crawlers can now a days parse javascript, it is still massive damage to SEO of the website. There is also compatibility and overheads issues, which can be issue with today mobile devices, netbooks etc.