Merge pull request #36 from f/master
Add test to make custom renderer macros for projects and add documentation to views.
This commit is contained in:
commit
b8e7ad2577
2 changed files with 56 additions and 0 deletions
|
@ -13,3 +13,45 @@ And you should have an `hello.ecr` view. It will have the same context as the me
|
||||||
```erb
|
```erb
|
||||||
Hello <%= env.params["name"] %>
|
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
|
||||||
|
```
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
require "./spec_helper"
|
require "./spec_helper"
|
||||||
|
|
||||||
|
macro render_with_base_and_layout(filename)
|
||||||
|
render "spec/asset/#{{{filename}}}", "spec/asset/layout.ecr"
|
||||||
|
end
|
||||||
|
|
||||||
describe "Views" do
|
describe "Views" do
|
||||||
it "renders file" do
|
it "renders file" do
|
||||||
kemal = Kemal::Handler.new
|
kemal = Kemal::Handler.new
|
||||||
|
@ -11,6 +15,16 @@ describe "Views" do
|
||||||
response.body.should contain("Hello world")
|
response.body.should contain("Hello world")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "renders file with dynamic variables" do
|
||||||
|
kemal = Kemal::Handler.new
|
||||||
|
kemal.add_route "GET", "/view/:name" do |env|
|
||||||
|
render_with_base_and_layout "hello.ecr"
|
||||||
|
end
|
||||||
|
request = HTTP::Request.new("GET", "/view/world")
|
||||||
|
response = kemal.call(request)
|
||||||
|
response.body.should contain("Hello world")
|
||||||
|
end
|
||||||
|
|
||||||
it "renders layout" do
|
it "renders layout" do
|
||||||
kemal = Kemal::Handler.new
|
kemal = Kemal::Handler.new
|
||||||
kemal.add_route "GET", "/view/:name" do |env|
|
kemal.add_route "GET", "/view/:name" do |env|
|
||||||
|
|
Loading…
Reference in a new issue