HTML模板如何适应后端语言和数据库?(How does HTML templating fit in with the backend language and database?)

如果这个问题不够具体,我很抱歉,但我确实需要一些帮助来理解这个概念。 我一直在研究许多Javascript库,包括JQuery,MooTools,Backbone,Underscore,Handlebars,Mustache等 - 还有Node.js和Meteor(我知道所有这些都有不同的用途)。 我对每个人的作用有基本的了解,但我的问题主要集中在模板库上。

我认为一般的想法是模板将由从服务器检索的JSON对象填充。 但是,我对JSON对象的形成方式感到困惑,如果它可以通过另一种方式到后端来更新数据库。 如果这不正确,请纠正我。

举一个更实际的例子,假设我在Linux上运行Apache,并使用MongoDB作为数据库,使用python作为我的主要语言。 所有这些组件如何与模板库和彼此交互?

例如,如果我有一个包含表单的HTML文件,则操作将设置为某个python脚本; 该脚本是否必须检索字段,验证它们,然后在数据库中更新它们? 如果它是MySQL,我必须编写一个SQL语句来更新它,但是Mongo不会有不同/更容易,因为它是基于BSON / JSON的吗?

而对于另一个例子,假设我有一个view-account.html页面需要从数据库中提取用户信息,它将以什么形式提取信息以及如何将其填入模板? 我猜我必须有一个python脚本从数据库中提取信息,创建一个JSON对象,并使用它来填充html模板中的字段。

我知道有一些网络框架可以简化这个过程,请建议您推荐的任何内容; 但是,我真的很想了解这些组件如何相互作用的概念。

谢谢!!

I apologize if this question is not specific enough, but I do need some help understanding this concept. I've been researching many Javascript libraries including JQuery, MooTools, Backbone, Underscore, Handlebars, Mustache, etc - also Node.js and Meteor (I know all those serve different purposes). I have a basic idea of what each does, but my question is mainly focused on the templating libraries.

I think the general idea is that the template will be filled by a JSON object that's retrieved from the server. However, i'm confused by how that JSON object is formed, and if it can go the other way to the backend to update the database. Please correct me if this is incorrect.

For a more solid example, let's say I have Apache running on Linux, and am using MongoDB as the database and python as my primary language. How do all these components interact with the templating library and each other?

For example, if I have an HTML file with a form in it and the action will be set to some python script; will that script have to retrieve the fields, validate them, and then update them in the DB? If it's MySQL I'd have to write a SQL statement to update it, but with Mongo wouldn't it be different/easier since it's BSON/JSON based?

And for the other example, let's say I have a view-account.html page that will need to pull up user information from the DB, in what form will it pull the information out and how will it fill it into the template? I'm guessing i'd have to have a python script that pulls the information from the DB, create a JSON object, and use it to populate the fields in the html template.

I am aware there are web frameworks that will ease this process, and please suggest any that you would recommend; however, I'm really interested in understanding the concepts of how these components interact.

Thanks!!

最满意答案

显然有很多方法可以一起工作,但听起来你有(a)正确的想法。 通常,前端处理JSON,服务器提供JSON。 消费或提供这些回复的内容无关紧要; 您不必担心Mongo是您的数据库,或者下划线正在处理您的模板。

将您的前端和后端视为两个完全独立的应用程序(这非常正确)。 忽略这样一个事实,即您的前端代码和模板可能是从处理后端的同一台机器上提供的。 你的后端是持久数据的业务,也是你展示它的业务的前端。

RE:使用JSON / BSON的Mongo; 它使用与您的前端相同的语言进行交流的事实是一个红色的鲱鱼。 无论如何,你的数据库层应该抽象出来,所以你只是使用Python dicts / tuples / etc与数据库进行通信。

我猜我必须有一个python脚本从数据库中提取信息,创建一个JSON对象,并使用它来填充html模板中的字段。

发现 :)

There are obviously many ways this can all work together, but it sounds like you have the (a) right idea. Generally the frontend deals with JSON, and the server provides JSON. What's consuming or providing those responses is irrelevant; you shouldn't need to worry that Mongo is your database, or that underscore is handling your templates.

Think of your frontend and backend as two totally separate applications (this is pretty much true). Ignore the fact that your frontend code and templates are probably delivered from the same machine that's handling the backend. Your backend is in the business of persisting data, and your frontend in the business of displaying it.

RE: Mongo using JSON/BSON; The fact it uses the same language as your frontend to communicate is a red herring. Your DB layer should abstract this away anyway, so you're just using Python dicts/tuples/etc to talk to the database.

I'm guessing i'd have to have a python script that pulls the information from the DB, create a JSON object, and use it to populate the fields in the html template.

Spot on :)

更多推荐