Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Grabli66/chocolate
Simple web framework and template engine
https://github.com/Grabli66/chocolate
Last synced: 2 months ago
JSON representation
Simple web framework and template engine
- Host: GitHub
- URL: https://github.com/Grabli66/chocolate
- Owner: Grabli66
- License: mit
- Created: 2015-10-03T09:45:12.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2015-10-25T09:53:39.000Z (over 8 years ago)
- Last Synced: 2024-01-31T09:58:57.137Z (5 months ago)
- Language: Crystal
- Size: 188 KB
- Stars: 10
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Lists
- awesome-crystal - chocolate - Simple web framework and template engine (Web Frameworks)
README
# chocolate
Simple web framework and template engine
## Installation
Add this to your application's `shard.yml`:
```yaml
dependencies:
chocolate:
github: Grabli66/chocolate
```## Usage
Need latest crystal compiler to compile some samples.
### Hello world
```crystal
require "chocolate"include Zephyr
include Chocolateget "/" do
"Hello, world!"
endlisten {
port 8080
}
```### Hello with template
```crystal
require "chocolate"include Zephyr
include Chocolateget "/" do
html {
head {
title(text: "Hello world")
}
body {
div(text: "Hello, world!")
}
}
endlisten {
port 8080
}
```
### Hello with view```crystal
require "chocolate"include Zephyr
include Chocolateclass HelloView
def render
html {
head {
title(text: "Hello world")
}
body {
div(text: "Hello, world!")
}
}
end
endget "/" do
HelloView.new
endlisten {
port 8080
}
```### Handle params
```crystal
require "chocolate"include Zephyr
include Chocolateget "/registration/success" do
html {
body {
div(css: "result", text: "Success")
}
}
endget "/blog/:id" do |req|
blog = Database.get_blog(req.params["id"])
blog.to_s
endpost "/registration/singup" do |req|
Database.save_user(req.params["email"], req.params["password"])
redirect("/registration/success")
end
```### Handle static files
```crystal
require "chocolate"
include chocolate
include zephyrlisten {
port 8080
static_dir "./static"
}
```### Handle errors
```crystal
require "chocolate"include Zephyr
include Chocolateon_exception ResourceNotFoundException do
"NOT FOUND"
endon_exception Exception do
"INTERNAL ERROR"
endlisten {
port 8080
}
```### JSON response
```crystal
require "json"
require "chocolate"include Zephyr
include Chocolateclass Database
def self.get_user(id)
User.new(1, "John", "Doe")
end
endclass User
JSON.mapping({
id: Int32,
name: String,
email: String
})def initialize(@id, @name, @email)
end
endget "/user/:id" do |req|
user = Database.get_user(req.params["id"] as String)
json(user)
endlisten {
port 8080
}
```### View inheritance
```crystal
require "chocolate"include Zephyr
include Chocolateabstract class RootView < View
abstract def render_contentdef render
html {
head {
title(text: @title)
}
body {
header {
ul(css: "menu") {
li(text: "Home") {
css_add("active") if @location == :home
}
li(text: "Blog") {
css_add("active") if @location == :blog
}
}
}include_element(render_content)
}
}
end
endclass HomeView < RootView
def initialize
@title = "Home"
@location = :home
enddef render_content
div(css: "home") {
h1(text: "HOME")
}
end
endclass BlogView < RootView
def initialize
@title = "Blog"
@location = :blog
enddef render_content
div(css: "blog") {
h1(text: "Blog")
}
end
endget "/" do
HomeView.new
endget "/blog" do
BlogView.new
endlisten {
port 8080
}
```### Route group
```crystal
require "../chocolate/src/**"include Chocolate
include Zephyrclass SessionNotFoundException < Exception
endon_exception SessionNotFoundException do
"SESSION NOT FOUND"
endgroup do
before do |req|
raise SessionNotFoundException.new unless req.params["session_id"]?
endget "/user/:id" do |req|
req.params["id"] as String
endget "/blog/:id" do |req|
req.params["id"] as String
end
endlisten {
port 8080
}
```
## Contributing1. Fork it ( https://github.com/Grabli66/chocolate/fork )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
5. Create a new Pull Request## Contributors
- Grabli66 (https://github.com/Grabli66) Grabli66 - creator, maintainer