The view layout files are located in the
The default layout is set to be
main.erb, which is a file that is already included in Eucalypt. However, this can be changed in the core application file.
app/views/layouts/main.rb<html><head><title><%= @page_title %></title><%= manifest :stylesheet, :script %></head><body><%= yield %></body></html>
yield method - this is essential to include in any layout file, as it specifies where the rendered content should be placed in the view.
Suppose we have an index page that says
But this isn't a complete webpage without the
head tags (which were in the
If we were to render the index page by inserting the contents of
index.erb into the location of the
yield method call in
main.erb, and set the instance variable
'Welcome to the welcome page!', we would have a perfect webpage that looks like:
<html><head><title>Welcome to the welcome page!</title><%= manifest :stylesheet, :script %></head><body><h1>Hello World!</h1></body></html>
To render the
index.erb file using the
main.erb file as a layout (whenever we visit
/home), simply add the following resource handler in your controller:
get '/' do@page_title = 'Welcome to the welcome page!'erb :index, layout: :'layouts/main'end
But remember, since the default layout is
main.erb, we don't need to specify the layout.
This means we can get rid of the
layout keyword argument to the
erb rendering method, along with the
:'layouts/main' symbol that specifies the layout:
get '/' do@page_title = 'Welcome to the welcome page!'erb :indexend
For some views, you might want to render the entire page from a single view file (without a layout). This is often the case with pages that are unique and don't follow a pattern like having the same navigation bar as other pages. Such page examples are typically index pages, 404 pages etc.
To omit a layout, simply set
false as the keyword argument
error Sinatra::NotFound doerb :"404", layout: falseend