LoginSignup
6
6

More than 5 years have passed since last update.

Sinatraとhaml,slim

Last updated at Posted at 2013-05-04

--

Sinatraでhamlとslimを使ってみた。erbはまたそのうち。

--

ソースのリスト、ディレクトリ構成
├── test.rb
└── views
    ├── index.haml
    └── index.slim

requireしているものは普通にgem install xxx でOK
変数のスコープはよくわからないので適当。
test.rb
require 'sinatra'
require 'slim'
require 'haml'


get '/' do
        @arr01 = [0,1,2]
        @has01 = {:id => '1',:value => 'sample' }
        @has02 = [{:id => '1', :value => 'sample1'},
                  {:id => '2', :value => 'sample2'}]
        slim :index
end

get '/haml' do
        @arr01 = [0,1,2]
        @has01 = {:id => '1',:value => 'sample' }
        @has02 = [{:id => '1', :value => 'sample1'},
                  {:id => '2', :value => 'sample2'}]
        haml :index
end
テンプレートは以下のように作成し、ruby test.rb で起動。ブラウザでアクセスし動作確認する。
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]
views/index.haml
!!!
%html
  %head
    %title Haml haml haml!
  %body
    #header
      %h1 Haml!
    #content
      %p I love Haml
    #array
      %table
        %tr
          - @arr01.each do |item|
            %td= item
    #hash
      %table
        %tr
          %td= @has01[:id]
          %td= @has01[:value]
      %table
        - @has02.each do |item|
          %tr
            %td= item[:id]
            %td= item[:value]
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6