帮手:_
require 'sinatra'
require 'json'
require 'haml'
helpers do
def get_xml( *args )
# your code here…
end
end
get '/processed' do
# getxml is available here
status 200
meeting_dir="/home/default"
Dir.entries(meeting_dir)
end
现在,我假设您希望输出到 XML,因此该路由将不起作用,您将获得最后一个表达式的评估,即当前Dir.entries(meeting_dir)
.
设置一个视图,并使用Haml 的list_of
帮助器,我们将准备好输出值列表:
#views/contents.haml
<folders>
= list_of(@metas) do |meta|
= "<#{meta.name}>"
= meta.content
= "</#{meta.name}>"
<\folders>
现在你需要一个@metas
对象:
helpers do
def get_xml( meeting_dir )
dirs = Dir.entries(meeting_dir).reject{|e| e.start_with? "." }
meta = Struct.new(:name, :content)
dirs.each_with_object([]) do |d,metas|
meta.name = d # it's up to you whether you
# want the absolute or relative path here
meta.content = File.read File.expand_path(File.join d, "meta.xml")
metas << meta
end
# the return value of each_with_object
# is the object, so no need for a return expression
end
end
get '/processed', :provides => :xml do
# status 200 <- this is not needed
meeting_dir="/home/default"
@metas = get_xml(meeting_dir)
haml :contents, :layout => false
end
另请查看http://www.sinatrarb.com/intro.html#Conditions和http://www.sinatrarb.com/intro.html#Named%20Templates。这应该给你一些事情要做。