我正在创建一个超小型的Sinatra博客应用程序,我如何从数据库中获取条目,格式化它们并将它们插入到我的布局中?
答案 0 :(得分:25)
class Blog < Sinatra::Base
helpers do
def partial (template, locals = {})
erb(template, :layout => false, :locals => locals)
end
end
get "/list" do
@posts = Post.all
erb :list
end
end
<强> list.erb:强>
<% @posts.each do |post| %>
<%= partial(:post, :post => post) %>
<% end %>
<强> post.erb:强>
<h1><%= post.title %></h1>
<p><%= post.body %></p>
答案 1 :(得分:1)
<% @posts.each do |post| %>
<%= erb :"_partial_name", :locals => {} %>
<% end %>
部分模板需要以_
开头