A lightweight templating engine written in Go. Supports variables, loops, conditionals, and custom functions.
- Render templates with
{{ variable }}syntax - Conditional blocks:
{{if ...}} ... {{else}} ... {{end}} - Loops:
{{range ...}} ... {{end}} - File-based templates
- Extendable with custom functions
go get github.com/frontendninja10/go-template-enginepackage main
import (
"fmt"
"github.com/frontendninja10/go-template-engine/pkg/template"
)
func main() {
tmpl := "Hello, {{ Name }}!"
data := map[string]string{
"Name": "Frontend Ninja",
}
output, _ := template.Render(tmpl, data)
fmt.Println(output)
}Hello, Frontend Ninja!
Render a template with data from JSON:
gtpl examples/hello.tpl examples/data.jsonHello, {{ Name }}!
{{if IsAdmin}}
Welcome back, admin!
{{else}}
Welcome back, user!
{{end}}
Actions:
{{range Actions}}
- {{.}}
{{end}}
{
"Name": "Frontend Ninja",
"IsAdmin": true,
"Actions": [
"Create",
"Read",
"Update",
"Delete"
]
}Hello, Frontend Ninja!
Welcome back, admin!
Actions:
- Create
- Read
- Update
- Delete
- Write tests
- Add more examples
- Add more documentation
- Build a playground frontend
This section is for my personal notes, thought process, and decisions and is not part of the documentation.
After initializing the project, I proceed by creaing pkg and pkg/template directories, and a render.go file. This would contain logic to render output and would serve as a public API.
In the render.go file, I wrote the function signature of the Render function:
func Render(tmpl string, data map[string]string) (string, error) {
return "", nil
}This folder would contain ready-to-run demos.
The examples directory contains a hello.tpl and data.json for now.