https://github.com/mekramy/gotemplate
Filesystem based engine with extra pipes for go standard template.
https://github.com/mekramy/gotemplate
go go-template golang mekramy-go template
Last synced: 4 months ago
JSON representation
Filesystem based engine with extra pipes for go standard template.
- Host: GitHub
- URL: https://github.com/mekramy/gotemplate
- Owner: mekramy
- License: isc
- Created: 2025-02-07T15:30:27.000Z (4 months ago)
- Default Branch: master
- Last Pushed: 2025-02-10T16:35:14.000Z (4 months ago)
- Last Synced: 2025-02-10T17:36:07.846Z (4 months ago)
- Topics: go, go-template, golang, mekramy-go, template
- Language: Go
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GoTemplate
GoTemplate is a flexible and powerful wrapper for Go standard templating engine, designed to simplify the process of rendering HTML templates with layouts, partials and custom functions.
## Features
- In-Memory caching
- Layout based rendering
- Global partials view
- Support for development and production environments## Installation
To install GoTemplate, use `go get`:
```sh
go get github.com/mekramy/gotemplate
```## Template Syntax
For layout template you can use `{{ view }}` function to render child view. All partials template can accessed by `@partials/path/to/file` or `template-name`.
**NOTE**: Use `include` function to import instead of builtin `template` function to prevent errors.
**NOTE**: In `Render`, you can load multiple partial views. The first `layouts` value must be the layout, and if the view has a partial and no layout, an empty string must be passed as the first argument.
### Builtin functions
- `{{ view }}`: render child template in layout. If used in non-layout template generate error!
- `{{ exists "template name or path" }}`: check if template name or path exists.
- `{{ include "template name or path" (optional data) }}`: includes and executes a template with the given name or path and data if exists.
- `{{ require "template name or path" (optional data) }}`: includes and executes a template with the given name or path and data or returning an error if the template does not exist.## Usage
### Basic Example
```html
{{ define "site-header" }}
...
{{ end }}...
Home Page
{{ .Title }}
{{ define "title" }}Home Page{{ end }}
{{ if exists "title" }}
{{ include "title" }}
{{ else }}
My App
{{ end }}
{{- require "site-header" . }} {{- view }} {{- include
"@partials/sub/footer" }}
```
```go
package mainimport (
"os"
"github.com/mekramy/gotemplate"
"github.com/mekramy/gofs"
)func main() {
fs := gofs.NewDir("./views")
tpl := gotemplate.New(fs, gotemplate.WithPartials("parts"))err := tpl.Load()
if err != nil {
panic(err)
}http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := gotemplate.Ctx().Add("Title", "Hello, World!")
err = tpl.Render(w, "pages/home", data, "layout")
if err != nil {
// Render error page with no layout and two partial views
tpl.Render(w, "pages/errors", nil, "", "pages/err-partial/500", "pages/err-partial/contact")
}
})}
```### Custom Options
```go
tpl := gotemplate.New(fs,
gotemplate.WithRoot("."),
gotemplate.WithPartials("partials"),
gotemplate.WithExtension(".tpl"),
gotemplate.WithDelimeters("{{", "}}"),
gotemplate.WithEnv(true),
gotemplate.WithCache(),
gotemplate.WithUUIDPipe(),
gotemplate.WithTernaryPipe(),
)
```## API
### Template Interface
```go
type Template interface {
Load() error
Render(w io.Writer, view string, data interface{}, layouts ...string) error
Compile(name, layout string, data any) ([]byte, error)
}
```### Options
- `WithRoot(root string) Options`: Sets the root directory for templates.
- `WithPartials(path string) Options`: Sets the directory for partial templates.
- `WithExtension(ext string) Options`: Sets the file extension for templates.
- `WithDelimeters(left, right string) Options`: Sets the delimiters for template tags.
- `WithEnv(isDev bool) Options`: Sets the environment mode (development or production).
- `WithCache() Options`: Enables template caching.
- `WithPipes(name string, fn any) Options`: Registers a custom function (pipe) for templates.### Context
Helper struct to pass data to template.
```go
type Context struct {
data map[string]any
}func Ctx() *Context
func ToCtx(v any) *Context
func (ctx *Context) Add(k string, v any) *Context
func (ctx *Context) Map() map[string]any
```### Custom Pipes
- `WithUUIDPipe() Options`: Adds a UUID generation pipe.
- `WithTernaryPipe() Options`: Adds a ternary operation pipe.
- `WithNumberFmtPipe() Options`: Adds a number formatting pipe.
- `WithRegexpFmtPipe() Options`: Adds a regular expression formatting pipe.
- `WithJSONPipe() Options`: Adds a JSON formatting pipe.
- `WithDictPipe() Options`: Adds a dictionary creation pipe.
- `WithIsSetPipe() Options`: Adds a pipe to check if a value is set.
- `WithAlterPipe() Options`: Adds a pipe to alter a value.
- `WithDeepAlterPipe() Options`: Adds a pipe to deeply alter a value.
- `WithBrPipe() Options`: Adds a pipe to convert `\n` to `
`.## License
This project is licensed under the ISC License.