Sinatra Partial With Data?

I am making a super-small application for the Sinatra blog, how can I take records from a database, format them and paste them into my layout?

+4
source share
2 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> 
+25
source
 <% @posts.each do |post| %> <%= erb :"_partial_name", :locals => {} %> <% end %> 

the need for a partial template starts with _

+1
source

Source: https://habr.com/ru/post/1305175/


All Articles