...

/

Creating HTML Templates with Slim

Creating HTML Templates with Slim

Learn to use Slim to create HTML templates.

We'll cover the following...

Slim template

Slim is a templating language that can replace ERB. It’s designed to require much less code to achieve the same results, and it does this by using a nested structure instead of HTML tags. Consider this ERB:

Press + to interact
<h2><%= t('.title') %></h2>
<table>
<%= render(cart.line_items) %>
<tr class="total_line">
<td colspan="2">Total</td>
<td class="total_cell"><%= number_to_currency(cart.total_price) %></td>
</tr>
</table>

In Slim, this would look like so:

Press + to interact
h2
= t('.title') table
= render(cart.line_items)
tr.total_line
td.colspan=2
Total
td.total_cell
= number_to_currency(cart.total_price)

Slim treats each line as an opening HTML tag, and anything indented under that line will be rendered inside that tag. Helper methods and instance variables can be accessed using =, like so:

Press + to interact
ul
li = link_to @product.name, product_path(@product)

To execute logic such as looping ...