Давно хотів написати статтю про Haml, та все ніяк не доходили руки. Починаючи з 1.2 Sinatra підтримує новий шаблонізатор Slim. Вирішив познайомитися з ним, і він мені відразу сподобався. Slim взяв найкраще від Haml, і основне його завдання - зменшити об'єм коду. Шаблони Slim можуть виглядати як Haml, використовуючи ярлики id і class, але їх використання не є обов'язковим. Як і в Haml форматування тут відбувається відступами. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
doctype html | |
html | |
head | |
title Slim Test | |
meta name="keywords" content="slim, syntax" | |
javascript: | |
$(function() { | |
alert('Hello World'); | |
}); | |
haml: | |
#someid.someclass{:this => 'test'} Content in haml | |
erb: | |
<%= some_method(@request) %> | |
body | |
/ comment block | |
with multiple lines | |
This is another line | |
h1 = @page_title | |
p#notice.message | |
| Welcome to the the syntax test. | |
This file is to exercise the various markup. | |
This is another line | |
- unless @users.empty? | |
table | |
- for user in users do | |
tr | |
td.user id=some_ruby('ere', @rme) data-test="some text #{with @ruby}" = @post.name | |
- else | |
p There are no users. | |
/ Single comment line | |
#content Hello #{@user.name}! Welcome to the test page! | |
Try out Slim! | |
= function_with_many_parameters(:a, @variable, :option => 1) | |
p.text | |
' Another text block | |
with multiple lines | |
= link_to('Test', @site) | |
.text#footer | |
' Footer text block | |
with multiple lines |
Якщо Haml – це HTML на стероїдах, тоді Slim – це Haml на дієті.