Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pthethanh/tiny
Tiny helper for building tiny site with Go html/template.
https://github.com/pthethanh/tiny
go golang html html-template website
Last synced: 9 days ago
JSON representation
Tiny helper for building tiny site with Go html/template.
- Host: GitHub
- URL: https://github.com/pthethanh/tiny
- Owner: pthethanh
- License: mit
- Created: 2021-08-26T13:13:13.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-19T14:51:13.000Z (over 1 year ago)
- Last Synced: 2024-06-20T13:29:36.681Z (5 months ago)
- Topics: go, golang, html, html-template, website
- Language: Go
- Homepage:
- Size: 128 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tiny
Tiny helper for building tiny site with Go html/template quickly and easily.Example: [example](https://github.com/pthethanh/tiny/tree/main/examples)
One of the site built with `tiny`: https://pthethanh.herokuapp.com/
## Usage
### Template definition
- Use `[[]]` as template delimiter instead of `{{}}`.
- Data of each page will be available as below struct:
```
PageData struct {
MetaData MetaData
Authenticated bool
User Claims
Error error
Cookies map[string]*http.Cookie// additional data return from DataHandler.
Data interface{}
}
```
So, from template the page data can be accessed directly via the exposed properties:
```
[[.MetaData]]
[[.Authenticated]]
[[.User]]
[[.Error]]
[[.Cookies]]
[[.Data]]
```### Starting the server
```go
package mainimport (
"net/http""github.com/pthethanh/tiny"
)func main() {
if err := http.ListenAndServe(":8000", tiny.NewSite("index.yml")); err != nil {
panic(err)
}
}
```### Custom data handler
You can provide custom data handler by register it to the site using `SetDataHandler` method:
```
site := tiny.NewSite("index.yml")// Set custom data handler.
site.SetDataHandler("index", func(rw http.ResponseWriter, r *http.Request) interface{} {
return "hello"
})
```Once set, the custom data can be accessed via `.Data` from the template:
```This is the data from custom data handler [[.Data]]
```