kemal/docs/views.md

58 lines
1 KiB
Markdown
Raw Normal View History

# Views
2015-12-16 18:32:01 +00:00
You can use ERB-like built-in **ECR** views to render files.
2015-12-07 20:03:35 +00:00
```crystal
2015-12-16 18:32:01 +00:00
get '/:name' do
2015-12-07 20:03:35 +00:00
render "views/hello.ecr"
end
```
2015-12-16 18:32:01 +00:00
And you should have an `hello.ecr` view. It will have the same context as the method.
```erb
2015-12-16 18:32:01 +00:00
Hello <%= env.params["name"] %>
```
## Using Layouts
You can use **layouts** in Kemal. You should pass a second argument.
```crystal
get '/:name' do
render "views/subview.ecr", "views/layouts/layout.ecr"
end
```
And you should use `content` variable (like `yield` in Rails) in layout file.
```erb
<html>
<head>
<title><%= $title %></title>
</head>
<body>
<%= content %>
</body>
</html>
```
## Using Common Paths
Since Crystal does not allow using variables in macro literals, you need to generate
another *helper macro* to make the code easier to read and write.
```crystal
macro my_renderer(filename)
render "my/app/view/base/path/#{{{filename}}}.ecr", "my/app/view/base/path/layouts/layout.ecr"
end
```
And now you can use your new renderer.
```crystal
get '/:name' do
my_renderer "subview"
end
```