开发者

Sinatra Partial with data?

开发者 https://www.devze.com 2022-12-24 21:49 出处:网络
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?class Blog < Sinatra::Base

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?


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>


<% @posts.each do |post| %>
<%= erb :"_partial_name", :locals => {} %>
<% end %>

the partial template need start with _

0

精彩评论

暂无评论...
验证码 换一张
取 消