我应该在服务器端还是客户端渲染html ?

时间:2021-06-22 19:40:33

I am looking into Backbone JS and getting a little confused. I am used to compiling my page html on the server-side (using JADE) and then interacting with these elements using jQuery on the client-side. A lot of the backbone examples suggest starting from a blank html body and rendering content on the client-side.

我正在寻找骨干JS和有点困惑。我习惯于在服务器端编译页面html(使用JADE),然后在客户端使用jQuery与这些元素交互。许多主干示例建议从空白的html主体开始,并在客户端呈现内容。

This seems really strange to me!

这对我来说真的很奇怪!

Questions:

问题:

  1. Do I have to use client-side templating?

    我必须使用客户端模板吗?

  2. Can I use BackboneJS to control pre-written server-side compiled html?

    我可以使用BackboneJS来控制预先编写的服务器端编译的html吗?

1 个解决方案

#1


16  

The typical way of using Backbone is to do things client-side. You use client-side template rendering to bind model values to your small view templates. It is an approach to turning HTML/JS into a application development platform with an actual component model.

使用主干的典型方法是在客户端进行处理。使用客户端模板呈现将模型值绑定到小视图模板。它是一种将HTML/JS转换为具有实际组件模型的应用程序开发平台的方法。

BUT, that doesn't mean that you can't or shouldn't mix your worlds.

但是,这并不意味着你不能或者不应该混淆你的世界。

For example, I use Backbone with Rails. My Rails view renders server-side HTML which includes Backbone logic/views. Then, all of my AJAX-style view rendering happens using Backbone with models, views and templates and the models talk to JSON services that live in the Rails backend. The user doesn't see a page load until they need to leave the page to do something else. The experience is quite nice.

例如,我使用主干和Rails。我的Rails视图呈现服务器端HTML,其中包含主干逻辑/视图。然后,我的所有ajax风格的视图呈现都使用带有模型、视图和模板的主干,并且模型与驻留在Rails后端中的JSON服务进行对话。用户在需要离开页面去做其他事情之前不会看到页面加载。这次经历很愉快。

You can certainly take that example and render your templates server-side. I wouldn't go as far as to render every instance of the template server-side because templating engines (including the one built into Backbone/Underscore) are so powerful. But, if you wanted to seed your templates with server-side rendering, I could see how that might be useful.

您当然可以使用该示例并在服务器端呈现模板。因为模板引擎(包括内置在主干/下划线中的)非常强大,所以我不会对模板服务器端的每个实例进行渲染。但是,如果您希望通过服务器端呈现来创建模板,我可以看到这可能是有用的。

#1


16  

The typical way of using Backbone is to do things client-side. You use client-side template rendering to bind model values to your small view templates. It is an approach to turning HTML/JS into a application development platform with an actual component model.

使用主干的典型方法是在客户端进行处理。使用客户端模板呈现将模型值绑定到小视图模板。它是一种将HTML/JS转换为具有实际组件模型的应用程序开发平台的方法。

BUT, that doesn't mean that you can't or shouldn't mix your worlds.

但是,这并不意味着你不能或者不应该混淆你的世界。

For example, I use Backbone with Rails. My Rails view renders server-side HTML which includes Backbone logic/views. Then, all of my AJAX-style view rendering happens using Backbone with models, views and templates and the models talk to JSON services that live in the Rails backend. The user doesn't see a page load until they need to leave the page to do something else. The experience is quite nice.

例如,我使用主干和Rails。我的Rails视图呈现服务器端HTML,其中包含主干逻辑/视图。然后,我的所有ajax风格的视图呈现都使用带有模型、视图和模板的主干,并且模型与驻留在Rails后端中的JSON服务进行对话。用户在需要离开页面去做其他事情之前不会看到页面加载。这次经历很愉快。

You can certainly take that example and render your templates server-side. I wouldn't go as far as to render every instance of the template server-side because templating engines (including the one built into Backbone/Underscore) are so powerful. But, if you wanted to seed your templates with server-side rendering, I could see how that might be useful.

您当然可以使用该示例并在服务器端呈现模板。因为模板引擎(包括内置在主干/下划线中的)非常强大,所以我不会对模板服务器端的每个实例进行渲染。但是,如果您希望通过服务器端呈现来创建模板,我可以看到这可能是有用的。