How can I use views and layouts with Ruby and ERB (not Rails)?

前端 未结 4 548
闹比i
闹比i 2020-12-20 06:08

How can I use views and layouts with Ruby and ERB (not Rails)?

Today i\'m using this code to render my view:

def render(template_path, context = sel         


        
4条回答
  •  隐瞒了意图╮
    2020-12-20 06:10

    Thanks for all the answers!

    I solved it finally by doing this, I hope someone else also can find this code useful:

    def render_with_layout(template_path, context = self)
    template = File.read(template_path)
    render_layout do
      ERB.new(template).result(context.get_binding)
    end
    end
    
    def render_layout
    layout = File.read('views/layouts/app.html.erb')
    ERB.new(layout).result(binding)
    end
    

    And I call it like this:

    def index
    @books = Book.all
    body = render_with_layout('views/books/index.html.erb')
    [200, {}, [body]]
    end
    

    Then it will render my view, with the hardcoded (so far) layout..

提交回复
热议问题