Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinatra Partial with data?

Tags:

ruby

sinatra

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?

like image 848
BlakeWilliams Avatar asked Nov 27 '22 23:11

BlakeWilliams


1 Answers

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>
like image 57
Todd Yandell Avatar answered Dec 19 '22 04:12

Todd Yandell