简体   繁体   English

Rails渲染HTML是否比静态HTML慢?

[英]Is Rails rendering HTML slower than static HTML?

A Rails layout Rails布局

<%= image_tag("logo.png", :alt => "Sample App", :class => "round") %>

will render as the HTML 将呈现为HTML

<img alt="Sample App" class="round" src="/images/logo.png" />

Will web pages load noticeably slower if they have to render a page versus getting the HTML directly? 如果网页必须呈现页面而不是直接获取HTML,网页加载会明显变慢吗? I'm trying to figure out the advantage of writing HTML in Ruby, except perhaps for convenience? 我试图弄清楚用Ruby编写HTML的优势,也许是为了方便起见?

Advantage of dynamically generated HTML 动态生成HTML的优势

In your simple example, it's actually not important which one of your two options you use. 在您的简单示例中,使用两个选项之一实际上并不重要。 Having a templating system like ERB is however necessary if as you want to create dynamic content, like in this example: 但是,如果要创建动态内容,则必须具有诸如ERB之类的模板系统,例如以下示例:

<%= image_tag(@user.logged_in? ? "logged_in.png" : "not_logged_in.png", :class => "login_status") %>

You cannot do this with static HTML. 您不能使用静态HTML执行此操作。

Performance 性能

Dynamically generating the HTML like this before serving it will obviously take a bit longer than serving static HTML. 在提供它之前动态生成HTML显然比提供静态HTML需要更长的时间。 Rails has caching built in, however, so you can easily control the impact of this. 但是,Rails内置了缓存功能 ,因此您可以轻松控制其影响。

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

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