Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

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

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 = self)
 template = File.read(template_path)
 ERB.new(template).result(context.get_binding)
end

This works very well, but how can I implement the same function, but to render the template inside a layout? I want to call render_with_layout(template_path, context = self), and so that it will have a default layout.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since you tagged it with Sinatra I assume that you us Sinatra.

By default you view is rendered in your default layout called layout.erb

get "/" do
   erb :index
end

This renders your view index with the default layout.

If you need multiple layouts you can specify them.

get "/foo" do
   erb :index, :layout => :nameofyourlayoutfile
end

* If you don't use Sinatra you may want to borrow the code from there.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...