stephenwithav / template

Functional Options wrapper for text/template

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Purpose

text/template chains methods together, which prevents some useful code patterns from being employed. This package wraps text/template and provides an Options-style interface.

Example from sqlc's internal code:

template.New("table").
    Funcs(funcMap).
	ParseFS(
		template,
		"templates/*.tmpl",
		"templates/*/*.tmpl*",
	)

Rewritten in Options style:

template.New("table",
	template.Funcs(funcMap),
	template.ParseFS(
		template,
		"templates/*.tmpl",
		"templates/*/*.tmpl*",
	))

Why does this matter?

It improves code reuse, enabling custom functionality like that in kobra.

About

Functional Options wrapper for text/template


Languages

Language:Go 100.0%