建立与HTML模板与在JavaScript构建它?模板、HTML、JavaScript

2023-09-10 18:22:01 作者:繁华落尽√空城殇

最近,我开始做网络应用程序,而我做一些东西,工作,我不知道的最佳做法。

I recently started making web apps, and while I make some stuff that works, I'm not sure of the best practices.

到现在为止,我大部分时间使用的Django,web.py和PHP的模板系统。但现在,我使用jQuery和漂亮ajaxy魔法从服务器获取数据,而无需刷新客户端,我看到直接在JavaScript中构建HTML的优点(这样我就可以只送小JSON对象到客户端,让他弄清楚什么改变以及如何)。

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).

所以,现在我有一些位,并且使用模板渲染,有的认为是建立在JavaScript和连一个可怕的情况下,有二者混合,产生一个JavaScript函数,用于创建一个HTML表web.py模板 - 感觉就像C宏一遍! (我将重构的客场最终)

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)

这是Web开发一个共同的问题?任何推荐的最佳做法,如使用JSON的一切,使尽可能多的在JavaScript,使用foo库等?任何一个优秀的启发式什么来处理与模板,以及如何使用JavaScript处理?

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?

在这里搜索了一下,我发现有人asking关于JavaScript的模板,这似乎像一个可能的解决方案。

Searching a bit here, I found someone asking about javascript templates, which does seem like a possible solution.

推荐答案

请不要使用JavaScript来布局你的网页。只使用它需要的地方。

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

最近增强和标准化,Javascript是通常仍缓慢,很难做到完全兼容的(认为旧IE等),并与一些屏幕阅读器和刮削器/搜索引擎不那么兼容。

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.

如果你可以返回直线上升HTML做到这一点,那么就是通常你想要去的方式。

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