Simple system for writing HTML/XML as Go code. Better-performing replacement for html/template and text/template

Overview

Simple system for writing HTML/XML as Go code. Better-performing replacement for html/template and text/template. Vaguely inspired by JS library https://github.com/mitranim/prax, but uses a different design.

Features / benefits:

  • No weird special language to learn.
  • Use actual Go code.
  • Use normal Go conditionals.
  • Use normal Go loops.
  • Use normal Go functions.
  • Benefit from static typing.
  • Benefit from Go code analysis.
  • Benefit from Go performance.
  • Tiny and dependency-free (only stdlib).

TOC

Usage

API docs: https://pkg.go.dev/github.com/mitranim/gax.

Posts

Posts

Post0

Post1

} func render(E gax.E, dat Dat) { E(`html`, A{{`lang`, `en`}}, func() { E(`head`, nil, func() { E(`meta`, A{{`charset`, `utf-8`}}) E(`link`, A{{`rel`, `icon`}, {`href`, `data:;base64,=`}}) // Use normal Go conditionals. if dat.Title != "" { E(`title`, nil, dat.Title) } else { E(`title`, nil, `test markup`) } }) E(`body`, nil, func() { E(`h1`, A{{`class`, `title`}}, `Posts`) // Use normal Go loops. for _, post := range dat.Posts { E(`h2`, nil, post) } }) }) } var mockDat = Dat{ Title: `Posts`, Posts: []string{`Post0`, `Post1`}, } type Dat struct { Title string Posts []string } ">
package main

import (
  "fmt"
  "github.com/mitranim/gax"
)

type A = gax.A

func main() {
  bui := gax.Bui(gax.Doctype)

  render(bui.E, mockDat)

  fmt.Println(bui)
  // Posts

Posts

Post0

Post1

} func render(E gax.E, dat Dat) { E(`html`, A{{`lang`, `en`}}, func() { E(`head`, nil, func() { E(`meta`, A{{`charset`, `utf-8`}}) E(`link`, A{{`rel`, `icon`}, {`href`, `data:;base64,=`}}) // Use normal Go conditionals. if dat.Title != "" { E(`title`, nil, dat.Title) } else { E(`title`, nil, `test markup`) } }) E(`body`, nil, func() { E(`h1`, A{{`class`, `title`}}, `Posts`) // Use normal Go loops. for _, post := range dat.Posts { E(`h2`, nil, post) } }) }) } var mockDat = Dat{ Title: `Posts`, Posts: []string{`Post0`, `Post1`}, } type Dat struct { Title string Posts []string }

Performance

For mostly-static templates, Gax loses to html/template but remains more than fast enough. For anything dynamic, Gax seems to perform several times better. The more complicated a template is, the better it gets.

The benchmark in gax_bench_test.go is intentionally naive, avoiding some Gax optimizations in order to mimic actual user code.

go test -bench . -benchmem
cpu: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
Benchmark_static_gax-12             331562        3404 ns/op      1544 B/op       25 allocs/op
Benchmark_static_template-12       6006633       193.4 ns/op       480 B/op        3 allocs/op
Benchmark_dynamic_gax-12             69954       17127 ns/op      8872 B/op      162 allocs/op
Benchmark_dynamic_template-12         9532      131470 ns/op     61791 B/op     1373 allocs/op

Changelog

v0.1.4

Bui.Child also supports func(E).

v0.1.3

Added Bui.With and Ebui.

v0.1.2

Minor syntactic bugfix.

v0.1.1

Converted methods .WriteTo(*[]byte) methods to .Append([]byte) []byte for better compliance with established interfaces.

v0.1.0

Init.

License

https://unlicense.org

Misc

I'm receptive to suggestions. If this library almost satisfies you but needs changes, open an issue or chat me up. Contacts: https://mitranim.com/#contacts

Owner
Nelo Mitranim
Autodidact, senior/lead software developer.
Nelo Mitranim
Similar Resources

HTML template engine for Go

Ace - HTML template engine for Go Overview Ace is an HTML template engine for Go. This is inspired by Slim and Jade. This is a refinement of Gold. Exa

Jan 4, 2023

A strongly typed HTML templating language that compiles to Go code, and has great developer tooling.

A strongly typed HTML templating language that compiles to Go code, and has great developer tooling.

A language, command line tool and set of IDE extensions that makes it easier to write HTML user interfaces and websites using Go.

Dec 29, 2022

Universal JSON, BSON, YAML, CSV, XML converter with templates

Universal JSON, BSON, YAML, CSV, XML converter with templates

Universal JSON, BSON, YAML, CSV, XML translator to ANY format using templates Key features Various input formats (json, bson, yaml, csv, xml) Flexible

Dec 11, 2022

⚗ The most advanced CLI template on earth! Featuring automatic releases, website generation and a custom CI-System out of the box.

⚗ The most advanced CLI template on earth! Featuring automatic releases, website generation and a custom CI-System out of the box.

cli-template ✨ ⚗ A template for beautiful, modern, cross-platform compatible CLI tools written with Go! Getting Started | Wiki This template features

Dec 4, 2022

Made from template temporalio/money-transfer-project-template-go

Made from template temporalio/money-transfer-project-template-go

Temporal Go Project Template This is a simple project for demonstrating Temporal with the Go SDK. The full 20 minute guide is here: https://docs.tempo

Jan 6, 2022

Go-project-template - Template for a golang project

This is a template repository for golang project Usage Go to github: https://git

Oct 25, 2022

Go-api-template - A rough template to give you a starting point for your API

Golang API Template This is only a rough template to give you a starting point f

Jan 14, 2022

The powerful template system that Go needs

Plush Plush is the templating system that Go both needs and deserves. Powerful, flexible, and extendable, Plush is there to make writing your template

Dec 29, 2022

"to be defined" - a really simple way to create text templates with placeholders

tbd "to be defined" A really simple way to create text templates with placeholders. This tool is deliberately simple and trivial, no advanced features

Sep 27, 2022
Goview is a lightweight, minimalist and idiomatic template library based on golang html/template for building Go web application.

goview Goview is a lightweight, minimalist and idiomatic template library based on golang html/template for building Go web application. Contents Inst

Dec 25, 2022
A template to build dynamic web apps quickly using Go, html/template and javascript
A template to build dynamic web apps quickly using Go, html/template and javascript

gomodest-template A modest template to build dynamic web apps in Go, HTML and sprinkles and spots of javascript. Why ? Build dynamic websites using th

Dec 29, 2022
Wrapper package for Go's template/html to allow for easy file-based template inheritance.

Extemplate Extemplate is a small wrapper package around html/template to allow for easy file-based template inheritance. File: templates/parent.tmpl <

Dec 6, 2022
Fast, powerful, yet easy to use template engine for Go. Optimized for speed, zero memory allocations in hot paths. Up to 20x faster than html/template

quicktemplate A fast, powerful, yet easy to use template engine for Go. Inspired by the Mako templates philosophy. Features Extremely fast. Templates

Dec 26, 2022
mold your templated to HTML/ TEXT/ PDF easily.
mold your templated to HTML/ TEXT/ PDF easily.

mold mold your templated to HTML/ TEXT/ PDF easily. install go get github.com/mayur-tolexo/mold Example 1 //Todo model type Todo struct { Title stri

Jun 7, 2019
Package damsel provides html outlining via css-selectors and common template functionality.

Damsel Markup language featuring html outlining via css-selectors, extensible via pkg html/template and others. Library This package expects to exist

Oct 23, 2022
Golang Echo and html template.

golang-website-example Golang Echo and html template. move GitHub repository for hello to golang-website-example Visual Studio Code Run and Debug: lau

Feb 4, 2022
Fusozay Var Var: A CLI tool for quick text template rendering

fvv - Fusozay Var Var A CLI tool for quick text template rendering Fusozay Var Var means "have fun" It is a reference to something I see a lot Fusozay

Dec 11, 2021
Api-go-template - A simple Go API template that uses a controller-service based model to build its routes

api-go-template This is a simple Go API template that uses a controller-service

Feb 18, 2022
Code your next Go web project with (a) Mojito! No matter if its an API or a website, go-mojito assists you with dependency injection, simple routing, custom request / response objects and template rendering
 Code your next Go web project with (a) Mojito! No matter if its an API or a website, go-mojito assists you with dependency injection, simple routing, custom request / response objects and template rendering

Go-Mojito is a super-modular library to bootstrap your next Go web project. It can be used for strict API-only purposes as well as server-side renderi

May 1, 2022