有道基于AJAX的输入来构建HTMLAJAX、HTML

2023-09-10 17:30:07 作者:如今的你又在谁的身旁

所以,我正在开发在Django该Web应用程序。确切的Web框架无所谓,但问题是:我们之间的code,数据和实际的HTML一个很好的分离。我们去然而,进一步的,我们越发现我们想保持一个单一的网页上,并通过AJAX请求的用户操作界面响应。现在我发现自己写它期望从AJAX请求一个特定的输入和基本字符串连接和数据构建页面的大部分地区所有这些处理函数。突然,它是1999年再次,我手动创建HTML字符串。这不可能吧?

So I'm developing this web application in Django. The exact web framework doesn't matter, but the point is: We have a nice separation between code, data and the actual HTML. The further we go however, the more we find we'd like to keep on a single web page and make the interface respond to user actions via AJAX requests. Now I find myself writing all these handler functions which expect a particular input from the AJAX request and construct large parts of the page by basically concatenating strings and data. Suddenly it's 1999 again and I'm manually creating HTML strings. This can't be it?

所以我的问题是,什么是一个不错的模式/框架/ ...创建在浏览器端以系统的方式HTML吗?我知道有一些模板插件jQuery的,之前我承诺其中的一个,但是我不知道是否有没有一个更根本的办法处理这一问题不能全是难得的?

So my question is, what's a decent pattern/framework/... to create HTML on the browser side in a systematic fashion? I'm aware there are some templating plug-ins for jQuery, before I commit to one of those however I wonder if there isn't a more fundamental approach to this problem that can't be all that rare?

推荐答案

我所做的以前是让我的服务器上做加工和code一代。保持负载关闭客户端

What I've done before is let my server do the processing and code generation. Keeps the load off the client.

您可以加载谐音(意见),并返回他们,JSON EN codeD或其他方式。如果使用JSON,使HTML在你的响应对象或类似的内容。

You could load partials (views) and return them, json encoded or otherwise. If you use json, make the HTML be "content" in your response object or something similar.

这样的话,有没有code复制,因为你可以使用相同的看法。诀窍就变成了如何把它们分开。

This way, there's no code duplication since you can use the same views. The trick becomes how to split them up.

 
精彩推荐