Search code examples
rubyerbyield

Ruby method that yields one ERB template over another


I'm need to know how write a method that takes a page ERB below

<h1>Hello World</h1>

and yield within a layout ERB template below

<html>
<head>
  <title>Layout</title>
</head>
<body>
  <%= yield %>
</body>
</html>

Edited:

I am actually looking for a simple method/function which devanand has referred me a link to. Below is what I had worked on, for I have no slightest idea on how to write a method for this.

page = "<h1>Hello World</h1>"

layout = "<html>
<head>
  <title>Layout</title>
</head>
<body>
  <%= yield %>
</body>
</html>"

def render_layout(layout)
  ERB.new(layout).result(binding)
end

def render_view_with_layout(view, layout)
  render_layout(layout) do 
    ERB.new(view).result
  end  
end

Solution

  • I hope I got you question. The following link should answer your question

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