问题
I recently started making web apps, and while I make some stuff that works, I'm not sure of the best practices.
Up to now, I've mostly used the templating systems of Django, web.py and PHP. But now that I'm using jQuery and nice ajaxy magic to get data from the server without refreshing the client, I'm seeing the advantages of building the HTML directly in javascript (so I can just send a small json object to the client and have him figure out what to change and how).
So now I have some bits that are rendered with templates, and some that are built in javascript, and even one horrid case where there's a mix of both, a web.py template that generates a javascript function that creates a HTML table - feels like C macros all over again! (I'll refactor that away eventually)
Is this a common problem in web development? Any recommended best practices, such as "use json for everything, render as much as possible in javascript", "use library foo", etc.? Any good heuristics for what to handle with templates, and what to handle with javascript?
Searching a bit here, I found someone asking about javascript templates, which does seem like a possible solution.
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
I got into the same trouble some time back. I have a webpage that renders something using web.py templates and it had ajax pagination. So it had to render the next page using javascript. I ended up extending web.py templating engine to generate a javascript function to render the same template output.
See this blog post for more details:
http://anandology.com/blog/javascript-templating-with-webpy/
回答5:
AngularJS is a popular solution for client-side templating, which I discovered years after asking this question. You can write HTML templates ("partials") a bit like you would in Django, and then have javascript fetch and interpret them as needed - so you get the best of both world, a dynamic website with clean separation of code and layout.
来源:https://stackoverflow.com/questions/4240026/building-html-with-templates-versus-building-it-in-javascript