Layouts
Layouts are a great way to keep your code DRY (Dont Repeat Yourself) For example lets say all your pages have the same header and footer throughout all the pages and all that changes is the content area do you go and copy the header and footer to every page No... we just make one main layout and then set content tag where you will show your view.
On The Route
Create a route
#src/views/pages/user.ecr
get "/user" do
name = "joe"
render "src/views/pages/user.ecr", "src/views/layouts/main.ecr"
end
Render Method
Macro | Value1 | value2 | Detail |
---|---|---|---|
render | src/views/hello.ecr | src/views/layouts/main.ecr | you pass down the ecr view that you want to render on that route and layout page |
view location | layout location |
On The View
Just put the code you want to show up for that view
#src/views/hello.ecr
<div class="content-area">
Hey, here goes my html
</div>
Use <%= content %>
tag to display the page content on the layout
On The Layout
#src/views/layouts/main.ecr
<html>
<head>
<title>My Kemal Application</title>
</head>
<body>
<header>
<ul class="menu">
<li><a href="#">Home</a></li>
<li><a href="#">Contact</a></li>
</ul>
</header>
<%= content %>
</body>
</html>