参考
- Ruby - Sinatraとhaml,slim - Qiita を実践してみた
環境準備
mkdir views
touch test.rb views/index.slim Gemfile
Gemfile
source "https://rubygems.org"
gem "sinatra"
gem "slim"
bundle install --path=vendor/bundler
ソースの準備
test.rb
require 'sinatra'
require 'slim'
get '/' do
@arr01 = [0,1,2]
@has01 = {:id => '1',:value => 'sample' }
@has02 = [{:id => '1', :value => 'sample1'},
{:id => '2', :value => 'sample2'}]
slim :index
end
views/index.slim
doctype html
html
head
title Slim Example
body
h1 スリムテスト
- unless @arr01.empty?
table
- for item in @arr01
tr
td= item
- unless @has01.empty?
table
tr
td= @has01[:id]
td= @has01[:value]
- unless @has02.empty?
table
- for item in @has02
tr
td= item[:id]
td= item[:value]
- インデントがタグのくくり。
実行
bundle exec ruby test.rb -o 0.0.0.0 -p 3000
- http://localhost:3000/ にアクセス
HTML2slim
書式
/
でコメントアウト長いテキストなど、改行してテキストを書くにはパイプ
|
を使用Middleman で Slimを使って、render partialしたい時 - Qiita
==
で部分テンプレートにして読み込める。