(・w・) (・w・) (・w・)
lib/mustache.rb at master from defunkt/mustache - GitHub や examples/hash.rb at master from defunkt/mustache - GitHub を見た感じだと Mustache.new して直に値をハッシュ的に追加できるみたい。
でも、なんとなくサブクラス化してみたいので、サンプルを書いてみた。
これならどこかで生成した Hash オブジェクトをセットするような使いかたもできるし。
$ uname -m -r -s -v
Darwin 10.6.0 Darwin Kernel Version 10.6.0: Wed Nov 10 18:13:17 PST 2010; root:xnu-1504.9.26~3/RELEASE_I386 i386
$ ruby -v
ruby 1.8.7 (2010-12-23 patchlevel 330) [i686-darwin10]
$ gem list mustache
*** LOCAL GEMS ***
mustache (0.99.1)
$ cat ./mustache_sample.rb
#!/usr/bin/env ruby
$KCODE='u'
require 'rubygems'
require 'mustache'
class Sample < Mustache
def initialize(h)
# self[key] = val な感じで (・w・)
h.each{|k,v|
self[k] = v
}
end
end
h = {
:symb => 'symbhoge',
'name' => 'namehoge'
}
s = Sample.new(h)
s.template_file = 'mustache_template.txt'
puts s.render
$ cat ./mustache_template.txt
symb: {{{symb}}}
name: {{{name}}}
$ ruby ./mustache_sample.rb
symb: symbhoge
name: namehoge
(・w・)っ (・w・)っ (・w・)っ {{ mustache }}
tags: ruby mustache zurazure
Posted by NI-Lab. (@nilab)