简体   繁体   English

服务器端或客户端{{mustache}}?

[英]Server-side or client-side {{mustache}}?

{{mustache}} is, in my opinion, a great template library. 在我看来, {{mustache}}是一个很棒的模板库。 The question I face is whether to use it in the client-side, or in the server-side. 我面临的问题是是在客户端还是在服务器端使用它。

I am developing an online magazine that will have a main page with one big article and the the rest of its articles in smaller size underneath. 我正在开发一个在线杂志,它将有一个主要页面,其中包含一篇大文章,其余文章的篇幅较小。

Eg 例如

+-------------------------------------------------+
|                                                 |
|             Big Article Image                   |
|                                                 |
|                                                 |
+-------------------------------------------------+
     Title
     Author

+------------+     +------------+     +-----------+
|   Image    |     |   Image    |     |   Image   |
+------------+     +------------+     +-----------+
  Title               Title              Title
  Author              Author             Author
...

Server-side option 服务器端选项

Using {{mustache}} in the server-side, as a PHP library: when the browser asks for the page, the server will complete the template and send it back. 在服务器端使用{{mustache}}作为PHP库:当浏览器请求页面时,服务器将完成模板并将其发回。
pros: 优点:

  • Loading times will be better. 加载时间会更好。 The browser, once it has received the html code will know which other resources it has to ask the server for. 浏览器一旦收到html代码,就会知道它要向服务器询问哪些其他资源。

cons: 缺点:

  • It will be difficult to integrate with client-side {{mustache}} because when the template is parsed, all the "mustache-tags" that were not matched are removed (I do not know if this can be easily and cleanly avoided). 很难与客户端{{mustache}}集成,因为在解析模板时,所有未匹配的“mustache-tags”都会被删除(我不知道是否可以轻松,干净地避免这种情况)。
  • I do not like to modify the interface from the server-side, I rather do it from the client-side, thus the 3-tier architecture seems more clear (you might differ with this). 我不喜欢从服务器端修改接口,我宁愿从客户端进行,因此3层架构似乎更清晰(你可能会有所不同)。
  • I do not have previous experience with server-side {{mustache}} . 我以前没有服务器端{{mustache}}

Client-side option 客户端选项

Instead of plain {{mustache}} I usually use ICanHas.js , which wraps {{mustache}} and makes it incredibly easy and comfortable: when the browser asks for the page, the HTML is sent, containing all th js code to ask the server for the JSON which contains the title, author, and filename of the image. 而不是普通{{mustache}}我通常使用ICanHas.js ,它包装{{mustache}}并使其非常简单和舒适:当浏览器要求页面时, HTML被发送,包含所有要求的js代码JSON的服务器,包含图像的标题,作者和文件名。
pros: 优点:

  • Enhances 3-tier architecture. 增强3层架构。
  • Things like infinite scrolling (an other ajax stuff) are super-simple to add. 像无限滚动(其他ajax的东西)这样的事情非常简单。

cons: 缺点:

  • Load time is worsen. 加载时间恶化。 The browser will need to receive the code, make a request for the JSON, and then ask the server for the resources discovered in that JSON (like the image filename). 浏览器需要接收代码,发出JSON请求,然后向服务器询问在该JSON发现的资源(如图像文件名)。

Question

Which one do you think, from your experience, is the best solution? 根据您的经验,您认为哪一个是最佳解决方案? Why? 为什么?

I'd like to add a couple of points to your pros & cons. 我想补充几点你的利弊。

Client side templating is more error-prone than server side 客户端模板比服务器端更容易出错

With all kind of browsers, versions, devices and security settings, things can quickly get messed up. 对于所有类型的浏览器,版本,设备和安全设置,事情很快就会搞砸。 The more javascript and client-side templating you have on the page, the more likely you'll have some users getting a screwed up page. 您在页面上拥有的javascript和客户端模板越多,您就越有可能让某些用户获得搞砸的页面。 Think of IE default compatibility settings for instance, a pain. 例如,考虑IE默认兼容性设置,这很痛苦。 With server side templating, you just have to check it once and be happy. 使用服务器端模板,您只需检查一次并感到高兴。

Client side templating is typically harder to debug than server side 客户端模板通常比服务器端更难调试

First, you typically don't notice it when a client gets an error in the browser, except if you have some reporting system. 首先,当客户端在浏览器中出现错误时,您通常不会注意到它,除非您有一些报告系统。 Then, you get some cryptic one-liner error message. 然后,您会得到一些神秘的单行错误消息。 On server side, on the other hand, you can automatically monitor errors and have nice stack traces where it happen. 另一方面,在服务器端,您可以自动监视错误并在发生错误时获得良好的堆栈跟踪。 Sweet ...saves a lot of time. 甜蜜......节省了大量时间。

Probably better SEO with server side templating 可能更好的SEO与服务器端模板

It is straightforward for bots to accurately parse static or server generated pages. 机器人可以直接准确地解析静态或服务器生成的页面。 For pages full of client side templating, I don't really know what you'd get, hence the indexing might suffer. 对于充满客户端模板的页面,我真的不知道你会得到什么,因此索引可能会受到影响。

Loading time is quicker with server side templating 服务器端模板加载时间更快

Especially for mobile with low end phones, client side templating might make a noticeable difference. 特别是对于具有低端手机的移动设备,客户端模板可能会产生明显的差异。 Especially if you have to fetch the data in a second step after page load. 特别是如果您必须在页面加载后的第二步中获取数据。 Ajax + rendering on these small devices adds a small delay. 在这些小型设备上进行Ajax +渲染会增加一点延迟。 On server side with caching on the other side, you're pretty swift. 在服务器端,另一边有缓存,你很快。


That said, despite all disadvantages, client side templating has its purpose 尽管如此,尽管存在所有缺点,客户端模板仍有其目的

Client side templating rules with interactivity and two way data bindings 具有交互性和双向数据绑定的客户端模板规则

Typically for RIA (rich internet applications) where you read/edit/browse data in a very interactive fashion, client side templating can be very handy. 通常对于RIA(富Internet应用程序),您以非常交互的方式阅读/编辑/浏览数据,客户端模板可以非常方便。 It becomes a single page app, where the page is stateful and the adequate portions of the page is updated with corresponding data. 它成为单页面应用程序,其中页面是有状态的,并且页面的适当部分用相应的数据更新。 The downside is of course that such sites are harder to develop, maintain and more error-prone. 缺点当然是这些网站难以开发,维护并且更容易出错。

For interactive and complex user interfaces it makes sense to leverage the browser for template rendering. 对于交互式和复杂的用户界面,利用浏览器进行模板渲染是有意义的。 However in your case, it's sounds that your page is pretty static, so it boils down to your preference of spending more time coding server-side, or client-side? 但是在你的情况下,听起来你的页面是非常静态的,所以归结为你喜欢花更多时间编写服务器端或客户端?

Take note that rendering content in the client-side has SEO implications. 请注意,在客户端呈现内容具有SEO含义。 There are solutions and techniques to overcome this, but make sure you're aware of the effect of dynamically generated content and search-engines. 有解决方案和技术可以解决这个问题,但请确保您了解动态生成的内容和搜索引擎的效果。

When rendering on the client-side, if you're requesting the template and the JSON, that's two extra HTTP requests and that will definitely slow down the user experience. 在客户端进行渲染时,如果您正在请求模板和JSON,则这是两个额外的HTTP请求,这肯定会降低用户体验。 However you can preload the template and even the JSON in the initial page-load from the server and make things much faster, for example: 但是,您可以在服务器的初始页面加载中预加载模板甚至JSON,并使事情更快,例如:

<?php
$data = [ 'title': 'foo', 'content': 'lorem' ];
?>

<script id="tplArticle" type="text/template">
  <h1>{{title}}</h1>
  <p>{{content}}</p>
</script>

<div id="main"></div>

<script>
  var data = <?php echp json_encode($data) ?>
  var template = $('#tplArticle').html();
  var html = Mustache.to_html(template, data);
  $('#main').html(html);
</script>

This is a rough example of a PHP file that outputs the template and JSON on the first page-load from the server, and Javascript rendering the data and template on client-side without any extra HTTP request. 这是一个PHP文件的粗略示例,它在服务器的第一个页面加载时输出模板和JSON,Javascript在客户端呈现数据和模板,没有任何额外的HTTP请求。

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

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