├── .gitignore
├── LICENSE
├── README.md
├── example
├── main.go
└── views
│ ├── _layout.gohtml
│ ├── index.gohtml
│ └── info
│ ├── _layout.gohtml
│ └── about.gohtml
├── go.mod
└── view.go
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea
2 | .vscode
3 | .DS_Store
4 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2024 Aleksandr Baryshnikov
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # View [](https://godoc.org/github.com/reddec/view)
2 |
3 | The `view` package provides a type-safe and hierarchical (layouts) way to load and render Go HTML templates using the standard library's [`html/template`](https://pkg.go.dev/html/template) package. It supports loading templates from any sources exposed as [`fs.FS`](https://pkg.go.dev/io/fs#FS). The package comes with no external dependencies and is designed for use in web applications.
4 |
5 | This is extremly light library based on gist https://gist.github.com/reddec/312367d75cc03f1ee49bae74c52a6b31 and has zero external dependecies.
6 |
7 | Key points:
8 |
9 | - **Hierarchical**: The templates are loaded in a hierarchical way, allowing you to have a base layout and extend it with partials or views at different levels. Layouts defined in each directory as `_layout.gohtml` file and can be extended.
10 | - **Type-safe**: The package provides a type-safe wrapper around the standard `html/template` library using a custom `View` struct.
11 |
12 |
13 | ## [Example](example/)
14 |
15 | Layout
16 |
17 | ```
18 | ├── main.go
19 | └── views
20 | ├── _layout.gohtml
21 | ├── index.gohtml
22 | └── info
23 | ├── _layout.gohtml
24 | └── about.gohtml
25 | ```
26 |
27 |
28 | And the code
29 |
30 | ```go
31 | package main
32 |
33 | import (
34 | "embed"
35 | "fmt"
36 | "net/http"
37 |
38 | "github.com/reddec/view"
39 | )
40 |
41 | //go:embed all:views
42 | var views embed.FS
43 |
44 | func main() {
45 | index := view.Must(view.New[string](views, "views/index.gohtml"))
46 | about := view.Must(view.New[string](views, "views/info/about.gohtml"))
47 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
48 | index.Render(w, "the index page")
49 | })
50 | http.HandleFunc("/info/about", func(w http.ResponseWriter, r *http.Request) {
51 | about.Render(w, "made by RedDec")
52 | })
53 | fmt.Println("ready on :8080")
54 | panic(http.ListenAndServe(":8080", nil))
55 | }
56 | ```
57 |
58 | - note: `all:view` - the `all:` prefix is required in order to include files with underscore in name prefix
59 |
60 | ## Installation
61 |
62 | ```bash
63 | go get github.com/reddec/view
64 | ```
--------------------------------------------------------------------------------
/example/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "embed"
5 | "fmt"
6 | "net/http"
7 |
8 | "github.com/reddec/view"
9 | )
10 |
11 | //go:embed all:views
12 | var views embed.FS
13 |
14 | func main() {
15 | index := view.Must(view.New[string](views, "views/index.gohtml"))
16 | about := view.Must(view.New[string](views, "views/info/about.gohtml"))
17 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
18 | index.Render(w, "the index page")
19 | })
20 | http.HandleFunc("/info/about", func(w http.ResponseWriter, r *http.Request) {
21 | about.Render(w, "made by RedDec")
22 | })
23 | fmt.Println("ready on :8080")
24 | panic(http.ListenAndServe(":8080", nil))
25 | }
26 |
--------------------------------------------------------------------------------
/example/views/_layout.gohtml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |