I am making a super small Sinatra blog application, how could I take entries from a database, format them, and insert them into my layout?
+7
A:
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>
Todd Yandell
2010-03-26 01:52:31
Thanks a lot, that fixes everything.
TheMoonMaster
2010-03-26 01:57:53
Nice, concise answer +1
Chris McCauley
2010-03-26 07:04:23