20th Tue
haml で in file template
haml は構造化文章をシンプルかつ強力に記述することができるが、
複数行のデータの扱いには以下の理由で不向きである。
* 行指向であるため適切なインデントの記述が強要される
* 頑張って埋め込んでも可読性が落ちて全体の構造が把握し辛くなる
これを改善するには、コード(構造情報)とデータの分離が必要になる。
## inline filter module
ruby>>
module Inline
include Haml::Filters::Base
def self.[](key)
@@data[key.to_s] rescue nil
end
def render(str)
@@data = Hash[*str.split(/^\s*@@\s*(\w+)\s*\n/m)[1..-1]]
return nil
end
end
<<--
上記を評価することで、
sinatra の in file template のようにデータ群を管理できるようになる。
## 入力(haml)
haml>>
%h1 Inline Filter
.describe= Inline[:describe]
.author created by #{Inline[:author]}
:inline
@@ describe
This filter easily separates structure and data.
You can use in-file-templates like sinatra.
@@ author
maiha@wota.jp
<<--
## 出力(html)
html>>
Inline Filter
This filter easily separates structure and data.
You can use in-file-templates like sinatra.
<<--
posted by
maiha on Tue 20 Oct 2009 at 20:39 with 0 comments