Давно хотів написати статтю про Haml, та все ніяк не доходили руки. Починаючи з 1.2 Sinatra підтримує новий шаблонізатор Slim. Вирішив познайомитися з ним, і він мені відразу сподобався. Slim взяв найкраще від Haml, і основне його завдання - зменшити об'єм коду.
Шаблони Slim можуть виглядати як Haml, використовуючи ярлики id і class, але їх використання не є обов'язковим. Як і в Haml форматування тут відбувається відступами.
Ось так виглядає приклад шаблону з використанням Slim:
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
Якщо Haml – це HTML на стероїдах, тоді Slim – це Haml на дієті.
Інсталяція:
$ gem install slim
Список операторів:
|
Вертикальна риска повідомляє шаблонізатору, що потрібно просто вивести текст. При цьому всі "небезпечні" символи фільтруються.
'
Одинарна лапка працює як і попередній оператор, але додає в кінці пробіл.
-
Дефіс працює як в Haml, використовується для циклів, умов та іншого.
=
Знак рівності використовується для виводу результату виконання коду Ruby в html.
='
Працює як і попередній оператор, але додає в кінці пробіл.
==
Працює як і знак рівності, але виводить текст "як є", без обробки методом escape_html
=='
Працює як і попередній оператор, але додає в кінці пробіл.
/
Код не буде виконуватись і не потрапить в html взагалі.
/!
Знак для html коментарів (<!-- -->), які потраплять у вивід.
Атрибути та коментарі.
Позначати id і class можна ось так:
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
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
Крім того, Slim допускає кілька варіантів синтаксису:
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
Допускається на вибір використання наступних обгорток для атрибутів: {...}, [...], (...)
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
Ще одна приємна штука - динамічний контент в атрибуті. Якщо в атрибуті не вказані лапки, буде використана змінна.
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
Подобається, як працюють коментарі. Якщо є блок коду, який потрібно закоментувати, достатньо додати всього один рядок, що вплине на весь блок:
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
Немає коментарів:
Дописати коментар