Introduction

We all know the difference between a static website and a dynamic one. A static website has its HTML content ready and it is served by the web server immediately, as is. On the other hand, a dynamic website prepares its pages on the fly. Parts of the page are ready in advance, but other parts of it are prepared the moment the web page is requested by the client.

When you develop a dynamic website (or web application) you usually need to work with a web framework, like Django or Ruby on Rails. These frameworks allow you to write a web page that includes both static and dynamic parts. You write the static parts using standard HTML. For the dynamic parts, you need to use a special language and interpolate its statements inside the HTML content.

For example, when it comes to Django, you need to write things like these:

You can see this being an HTML document but with some statements that make sure the content is built dynamically. The {% if latest_question_list %} for example, starts an if block, which embeds the <ul> list when the condition in the if is true.

An equivalent Ruby on Rails example would have been something like this:

In this example above, you can see an HTML-content file, but with some parts starting with <% and ending with %>. The statements inside these blocks are Ruby code and they are executed by the framework when dynamically building the HTML content to return back to the client, i.e. the browser.

Tools like these are very useful and ready to use in any modern web framework. The HTML content of the pages of your web application are rarely static. So, you understand why every web framework needs to offer this feature, in one way or another.

We call this feature a template language. Because it allows you to create HTML templates and then use the template language to dynamically create different instances of HTML pages to be returned back to the requesting browser.

As we said above, Ruby on Rails allows you to write Ruby inside your HTML content. This is called Embedded Ruby and that’s why the files you actually create have the extension .html.erb. You write your HTML with Ruby embedded in them.

But Embedded Ruby is not a feature of Ruby on Rails. Embedded Ruby is part of the Ruby standard library. You can read its documentation here. It is only that Ruby on Rails uses it to allow you to dynamically build your web pages.

#template #erb #ruby-on-rails #ruby

Use Embedded Ruby as a Template Language
1.25 GEEK