haml は構造化文章をシンプルかつ強力に記述することができるが、 複数行のデータの扱いには以下の理由で不向きである。

  • 行指向であるため適切なインデントの記述が強要される
  • 頑張って埋め込んでも可読性が落ちて全体の構造が把握し辛くなる

これを改善するには、コード(構造情報)とデータの分離が必要になる。

inline filter module

   1  module Inline
   2    include Haml::Filters::Base
   3   
   4    def self.[](key)
   5      @@data[key.to_s] rescue nil
   6    end
   7   
   8    def render(str)
   9      @@data = Hash[*str.split(/^\s*@@\s*(\w+)\s*\n/m)[1..-1]]
  10      return nil
  11    end
  12  end

上記を評価することで、 sinatra の in file template のようにデータ群を管理できるようになる。

入力(haml)

   1  %h1 Inline Filter
   2  .describe= Inline[:describe]
   3  .author created by #{Inline[:author]}
   4   
   5  :inline
   6   @@ describe
   7   This filter easily separates structure and data.
   8   You can use in-file-templates like sinatra.
   9   
  10   @@ author
  11   maiha@wota.jp

出力(html)

   1  <h1>Inline Filter</h1>
   2  <div class='describe'>
   3    This filter easily separates structure and data.
   4    You can use in-file-templates like sinatra.
   5  </div>
   6  <div class='author'>created by maiha@wota.jp</div>

posted by Png maiha on Tue 20 Oct 2009 at 20:59
Contents
haml で in file template
Comments
瀧内元気: MacOS版は以下にあります * [genki/ViMouse](https://githu... '23-1
KingofSmack: Here also good reads for this mobile applicatio... '14-5
Spencer: You don't have to re-compile it, this version w... '14-4
staiano: Any chance we can get a recompile for 10.9? '14-1
dsjf: https://gist.github.com/6bf1bf2c3cbb5eb6e7a7 これ... '13-1
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ