mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Add test to make custom renderer macros for projects.
Add documentation
This commit is contained in:
		
							parent
							
								
									7b85aa2b45
								
							
						
					
					
						commit
						2d4edc83dc
					
				
					 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 | ||||
| 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" | ||||
| 
 | ||||
| macro render_with_base_and_layout(filename) | ||||
|   render "spec/asset/#{{{filename}}}", "spec/asset/layout.ecr" | ||||
| end | ||||
| 
 | ||||
| describe "Views" do | ||||
|   it "renders file" do | ||||
|     kemal = Kemal::Handler.new | ||||
|  | @ -11,6 +15,16 @@ describe "Views" do | |||
|     response.body.should contain("Hello world") | ||||
|   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 | ||||
|     kemal = Kemal::Handler.new | ||||
|     kemal.add_route "GET", "/view/:name" do |env| | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue