简体   繁体   English

客户端和服务器端页面生成

[英]client side and server side page generation

I am trying to use ajax to generate pages on the client side, and so far I got two ideas of doing it: 我正在尝试使用ajax在客户端生成页面,到目前为止,我有两个想法可以做到这一点:

  1. I can load a page with basic DOMs, JS and CSS files from server, then I can make an ajax call to get data from the server and generate pages on the client side. 我可以从服务器加载包含基本DOM,JS和CSS文件的页面,然后可以进行ajax调用以从服务器获取数据并在客户端生成页面。

  2. I can load a completed page with JS and CSS files from server, and I can make an ajax call whenever users want to update the contents. 我可以从服务器加载包含JS和CSS文件的完整页面,并且每当用户想要更新内容时都可以进行ajax调用。

I don't like either of the two methods (both of them will have page templates in JS code). 我不喜欢这两种方法中的任何一种(它们都将在JS代码中具有页面模板)。 The first one is making extra an request to the server; 第一个是向服务器发出额外的请求;第二个是向服务器发出请求。 the second one requires me to make another 'copy' of template in the php code. 第二个要求我在php代码中制作模板的另一个“副本”。

Is there any suggestion to make the structure cleaner? 有什么建议可以使结构更清洁吗?

UPDATE: I feel maybe it's good to generate the whole page at the server side when the page is not too big (i consider a table with hundreds of rows is big and it cost a lot for server to generate all those html tags around the data), and in this case, you just need to use ajax to pull the page instead of getting json data from the server then generate the page. 更新:我觉得在页面不太大的情况下在服务器端生成整个页面可能是个好主意(我认为具有数百行的表很大,并且服务器在数据周围生成所有这些html标签的成本很高),在这种情况下,您只需要使用ajax即可拉出页面,而不是从服务器获取json数据,然后生成页面。

From my experience, there are two scenarios in which you'd want to render client-side: 根据我的经验,您要在两种情况下呈现客户端:

  • You are building a pure client-side JS application. 您正在构建一个纯客户端JS应用程序。
  • You want to serve the data for different platforms (web, desktop, mobile native). 您想要为不同平台(Web,桌面,移动本机)提供数据。

Unless you identify your page with one of the previous scenarios (which I assume you don't), I recommend you to render in server side. 除非您使用以前的一种情况(我假设您没有)来标识您的页面,否则建议您在服务器端进行渲染。 Although the response is going to be bigger (server side computing time is negligible), the perceived speed compared to client side rendering is going to be faster. 尽管响应会更大(服务器端计算时间可以忽略),但是与客户端渲染相比,感知的速度会更快。

If you need to serve a very big page, note that you could autoload chunks of HTML via AJAX whenever the user reaches the bottom of the page without the need of client side templates. 如果需要提供很大的页面,请注意,只要用户到达页面底部,就可以通过AJAX自动加载HTML块,而无需客户端模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM