Skip to content

Commit

Permalink
feat : more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
noelukwa committed Dec 8, 2022
1 parent 6bd702c commit 72ff3ff
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
## tempest
# tempest
*Made out of neccessity and frustration* 😩

### Lets you
## Features
- Use go templates in your app without repeating the parsing logic over and over.
- Use any template supported by go html/template package.
- Use `go:embed` to embed template files in your binary.
- Parse templates once.

### How
## Usage
In order for tempest to parse templates, three conditions must be met.
1. Templates must be embeded
2. The name of the template used for layouts should be `layouts.<extention>`, otherwise, it should be stated with custom config.
Expand All @@ -22,7 +22,7 @@ In order for tempest to parse templates, three conditions must be met.
- Go version >= 1.16


### Example
## Example
Lets say you have a folder structure like this
```
.
Expand Down Expand Up @@ -77,7 +77,7 @@ func main() {
}
```

### Template Directory Parsing
## Template Directory Parsing
The template files in the `templates` directory above will be grouped as follows

```
Expand Down
62 changes: 62 additions & 0 deletions tempest.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,59 @@
// Copyright (c) 2022 Noel Ukwa. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be found
// in the LICENSE file.

// Package tempest is a simple template grouping helper for GO.
//
// It is intended to be used in conjuction with go html/template package,
// and not a replacement for the html/template package.
//
// A simple example:
//
// package main
//
// import (
// "fmt"
// "html/template"
// "log"
// "net/http"
// "os"
//
// "github.com/noelukwa/tempest"
// )
//
// //go:embed views
// var views embed.FS
//
// func main() {
// t := tempest.New()
//
// templates, err := t.LoadFS(os.DirFS("views"))
// if err != nil {
// log.Fatal(err)
// }
//
// http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// err := templates["index"].Execute(w, nil)
// if err != nil {
// log.Fatal(err)
// }
// })
//
// http.HandleFunc("/admin", func(w http.ResponseWriter, r *http.Request) {
// err := templates["admin/index"].Execute(w, nil)
// if err != nil {
// log.Fatal(err)
// }
// })
//
// fmt.Println("Listening on port 8080")
// log.Fatal(http.ListenAndServe(":8080", nil))
// }
//
// The above example will load all the templates in the views directory and
// subdirectories, and will group them into a map of templates to filenames.
//

package tempest

import (
Expand All @@ -9,6 +65,11 @@ import (
"strings"
)

// Config is the configuration for the tempest instance.
// It is used to set the file extension, the directory where the includes are
// stored, and the name used for layout templates.
// Defaults are set for each of these fields if tempest is initialized
// without a config.
type Config struct {
// The file extension of the templates.
// Defaults to ".html".
Expand All @@ -28,6 +89,7 @@ type tempest struct {
conf *Config
}

// New returns a new tempest instance with default configuration.
func New() *tempest {
return &tempest{
temps: make(map[string]*template.Template),
Expand Down

0 comments on commit 72ff3ff

Please sign in to comment.