英文:
What is the difference between template engines and server-side rendering?
问题
在2015年至2020年期间:Django、Flask、Laravel等等。所有这些框架都拥有相当不错的模板引擎。
但是在2018年至2020年期间,“现代”开发人员表示模板已经过时了。太多数据在服务器和客户端之间传输。只传输JSON数据并在客户端生成大量HTML代码更加有效。未来是单页面应用程序。Vue、React.js、Angular等等是新的标准。
现在是2023年,他们说SPA非常慢,占用了客户端的许多资源。良好的风格和杀手级功能是使用服务器端渲染。
我正在努力学习SSR是什么以及它是如何工作的。每次我看到的都是轮子已经回到了原始位置,而“现代”的SSR实际上就是一个针对Node.js后端的传统模板引擎。
真正的区别是什么?这两种方式都专注于返回预渲染的HTML数据。为什么只有Node.js才支持服务器端渲染?如果你搜索“服务器端渲染”,大多数示例将基于Node.js后端。但如果你搜索“Python服务器端渲染”,这里有一个使用Flask和“传统”模板引擎的例子。
英文:
In ~2015-2020: Django, Flask, Laravel, etc. All these frameworks had the pretty good template engines.
But in ~2018-2020 "modern" developers said that templates are old-fashioned. To much data is transferring between sever and client. It is much more effective to transfer only JSON-data and generate heavy HTML-code at client side. The future for single page applications. Vue, React.js, Angular, whatever is a new standard.
Nowadays it is 2023 and (they saying) SPA is very slow and using many resources at client side. The good style and killer-feature is a using of Server-Side Rendering.
I'm trying to learn what SSR is and how it works. And each time I see nothing but the wheel has returned to its original position and the "modern" SSR is a good old templates engine, but limited for Node.js backend.
What is the real difference? Both of ways are focused on returning pre-rendered HTML data. Why is Server-Side Rendering available only for Node.js? If you Google "Server-Side Rendering" most of the examples will be based on Node.js backend. But if you Google "Server-Side Rendering Python", here is a Flask and "legacy" template engine.
答案1
得分: 2
> 我正在尝试了解什么是SSR以及它是如何工作的。每次我看到的都只是轮子回到原位,而"现代"的SSR实际上是一个老式的模板引擎,但仅适用于Node.js后端。
是的,多多少少是这样的。有一些差异,但(1) SSR会从服务器上的数据组装HTML,即模板,以及(2) 这个术语仅在Node.js中使用(至少在我的经验中是这样)。
话虽如此,确实存在差异,这主要是源于历史的。在模板式渲染中,通常会将大部分逻辑写入HTML中,夹杂着一些调用(类似于php或perl风格的CGI),或者您会在HTML模板中写入占位符并填充它们(jinja2风格,就像在flask中一样)。当然,您不一定非要这样做。没有什么能阻止您编写一个使用字符串插值来构建页面的php站点,或者使用多个渲染的子模板,这些子模板包含共享业务逻辑,由动态开发的填充模板在django中汇总。但我认为我已经足够准确地描述了这些框架的通常风格。
对于'SSR',通常会有(i) 以SPA框架如Vue为基础的开发人员,其中'template'和'logic'通常在单位中更多或更少地组合在一起(Vue 'components'),并且语言(JS/TS)在某种程度上更接近结果DOM的结构,鼓励您考虑在python中可能要考虑两次的条件构建,以及(ii) 最初作为SPA启动并后来意识到实际上一切(或大多数)都可以提前或动态地渲染,即SPA框架并不适合站点最终要做的事情。我认为,由此产生的设计通常是可以明显识别的。您可以在django中编写相同的风格---有时从单个模板生成的基本站点会更干净---但是家族相似性是可见的。
因此,我认为这个问题(或这个答案)不仅仅是意见。这些是不同的工具,它们在不同的上下文中发展壮大,并且它们适用于稍微不同的问题。用于显示生成内容的页面的正确工具可能是CGI。用于许多网站的正确工具是静态站点。用于在服务器上拉回您的大型SPA时,当它变成一个引人注目的CGI界面时,可能是SSR。 (我相信它们也有积极的用途;))
另外,尽管我理解您对重新发明轮子的沮丧,但这只是在人类努力的所有领域都会发生的事情。每当轮子重新出现时,人们只需改进它,直到它的性能几乎与旧轮子一样好,赞美新的涂漆,然后继续前进
英文:
> I'm trying to learn what SSR is and how it works. And each time I see nothing but the wheel has returned to its original position and the "modern" SSR is a good old templates engine, but limited for Node.js backend.
Yes, more or less. There are some differences, but (1) SSR assembles html on the server from data about how to put that html together, i.e. templates and (2) the term is exclusively used (at least in my experience) for Node.js.
That said there is a difference, and it's largely genealogical. In template-style rendering you often write most of the logic into the html, interspersed with the odd callout (php or perl style CGI), or you write placeholders in the html template and populate them (jinja2 style, like in flask). Of course you don't have to do any of this. Nothing stops you writing a php site which uses string interpolation to build pages, or using multiple rendered sub-templates wrapped inside objects sharing business logic, pulled together by a dynamically developed populated template in django. But I think I've described the usual style of those frameworks accurately enough.
With 'SSR' you often have (i) developers who think in SPA frameworks like Vue, where 'template' and 'logic' often belong together in units which are composed more or less dynamically (Vue 'components'), and the language (JS/TS) is arguably closer to the structure of the resulting DOM, encouraging the kind of conditional building you'd have to think twice about in, say, python, and (ii) applications which started out as SPAs and have since realised that everything (or most) can in fact be rendered in advance or on the fly, i.e. the SPA framework wasn't a good fit for what the site ended up doing. The resulting design is, I think, frequently distinctly recognisable. You could write the same style in e.g. django---and sometimes a basic site generated from a single template would be a lot cleaner---but the family likeness is visible.
Thus I don't think this question (or this answer) is just opinion. These are different tools, they grew up in different contexts, and they suit slightly different problems. The right tool for a page which displays generated content is probably a CGI. The right tool for lots of websites is a static site. The right tool for pulling your massive SPA back onto the server when it turns out to be a glorified CGI interface might be SSR. (I'm sure there are positive uses for them too )
Separately, although I share your frustration about reinventing the wheel, this is just what happens in all domains of human effort. Whenever the wheel comes back one just has to improve it until it performs almost as well as the old wheel, admire the new paint, and move on
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论