Documentation ¶
Overview ¶
Package renderer implements data-driven templates for generating textual output
The renderer extends the standard golang text/template and sprig functions.
Templates are executed by applying them to a data structure (configuration). Values in the template refer to elements of the data structure (typically a field of a struct or a key in a map).
Actions can be combined using UNIX-like pipelines.
The input text for a template is UTF-8-encoded text in any format.
See renderer.ExtraFunctions for our custom functions.
Detailed documentation on the syntax and available functions can be found here:
Index ¶
- func ExtraFunctions() template.FuncMap
- func FromJSON(unmarshallable string) (interface{}, error)
- func FromYAML(unmarshallable string) (interface{}, error)
- func Gzip(input interface{}) (string, error)
- func JSONPath(expression string, marshallable interface{}) (interface{}, error)
- func MergeFunctions(dst *template.FuncMap, src template.FuncMap) error
- func N(start, end int) []int
- func ToYAML(marshallable interface{}) (string, error)
- func Ungzip(input interface{}) (string, error)
- func WithCryptFunctions() func(*config.Config)
- func WithDelim(left, right string) func(*config.Config)
- func WithExtraFunctions() func(*config.Config)
- func WithFunctions(extraFunctions template.FuncMap) func(*config.Config)
- func WithMoreFunctions(moreFunctions template.FuncMap) func(*config.Config)
- func WithMoreParameters(extraParams ...map[string]interface{}) func(*config.Config)
- func WithOptions(options ...string) func(*config.Config)
- func WithParameters(parameters map[string]interface{}) func(*config.Config)
- func WithSprigFunctions() func(*config.Config)
- type Renderer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtraFunctions ¶ added in v0.0.6
ExtraFunctions provides additional template functions to the standard (text/template) ones
func FromJSON ¶ added in v0.1.4
FromJSON is a template function, that unmarshalls JSON string to a map
func FromYAML ¶ added in v0.1.4
FromYAML is a template function, that unmarshalls YAML string to a map
func JSONPath ¶ added in v0.1.4
JSONPath is a template function, that evaluates JSONPath expression against a data structure and returns a list of results
Example (Array) ¶
package main import ( "fmt" "github.com/VirtusLab/render/renderer" "github.com/VirtusLab/render/renderer/parameters" ) func main() { json := `["Good Morning", "Hello World!"]` expression := "{$[1]}" params := parameters.Parameters{ "json": json, "expression": expression, } tmpl := ` {{ .json | fromJson | jsonPath .expression }} ` result, err := renderer.New( renderer.WithParameters(params), renderer.WithExtraFunctions(), ).Render(tmpl) if err != nil { fmt.Println(err) } fmt.Println(result) }
Output: Hello World!
Example (Multi) ¶
package main import ( "fmt" "github.com/VirtusLab/render/renderer" "github.com/VirtusLab/render/renderer/parameters" ) func main() { yaml := `--- data: en: "Hello World!" --- data: pl: "Witaj Świecie!" ` expression := "{$[*].data}" params := parameters.Parameters{ "yaml": yaml, "expression": expression, } tmpl := ` {{- range $m := .yaml | fromYaml | jsonPath .expression }} {{ range $k, $v := $m }}{{ $k }} {{ $v }}{{ end }} {{- end }} ` result, err := renderer.New( renderer.WithParameters(params), renderer.WithExtraFunctions(), ).Render(tmpl) if err != nil { fmt.Println(err) } fmt.Println(result) }
Output: en Hello World! pl Witaj Świecie!
Example (Multi_nested) ¶
package main import ( "fmt" "github.com/VirtusLab/render/renderer" "github.com/VirtusLab/render/renderer/parameters" ) func main() { yamlWithJson := `--- data: en.json: |2 { "welcome":{ "message":["Good Morning", "Hello World!"] } } --- data: pl.json: |2 { "welcome":{ "message":["Dzień dobry", "Witaj Świecie!"] } } ` expression := "{$[*].data.*}" expression2 := "{$.welcome.message[1]}" params := parameters.Parameters{ "yamlWithJson": yamlWithJson, "expression": expression, "expression2": expression2, } tmpl := ` {{- range $r := .yamlWithJson | fromYaml | jsonPath .expression }} {{ $r | fromJson | jsonPath $.expression2 }} {{- end }} ` result, err := renderer.New( renderer.WithParameters(params), renderer.WithExtraFunctions(), ).Render(tmpl) if err != nil { fmt.Println(err) } fmt.Println(result) }
Output: Hello World! Witaj Świecie!
Example (Simple) ¶
package main import ( "fmt" "github.com/VirtusLab/render/renderer" "github.com/VirtusLab/render/renderer/parameters" ) func main() { json := `{ "welcome":{ "message":["Good Morning", "Hello World!"] } }` expression := "{$.welcome.message[1]}" params := parameters.Parameters{ "json": json, "expression": expression, } tmpl := `{{ .json | fromJson | jsonPath .expression }}` result, err := renderer.New( renderer.WithParameters(params), renderer.WithExtraFunctions(), ).Render(tmpl) if err != nil { fmt.Println(err) } fmt.Println(result) }
Output: Hello World!
Example (Wildcard) ¶
package main import ( "fmt" "github.com/VirtusLab/render/renderer" "github.com/VirtusLab/render/renderer/parameters" ) func main() { json := `{ "welcome":{ "message":["Good Morning", "Hello World!"] } }` expression := "{$.welcome.message[*]}" params := parameters.Parameters{ "json": json, "expression": expression, } tmpl := ` {{- range $m := .json | fromJson | jsonPath .expression }} {{ $m }} {{- end }} ` result, err := renderer.New( renderer.WithParameters(params), renderer.WithExtraFunctions(), ).Render(tmpl) if err != nil { fmt.Println(err) } fmt.Println(result) }
Output: Good Morning Hello World!
Example (Yaml) ¶
package main import ( "fmt" "github.com/VirtusLab/render/renderer" "github.com/VirtusLab/render/renderer/parameters" ) func main() { yaml := `--- welcome: message: - "Good Morning" - "Hello World!" ` expression := "{$.welcome.message[1]}" params := parameters.Parameters{ "yaml": yaml, "expression": expression, } tmpl := `{{ .yaml | fromYaml | jsonPath .expression }}` result, err := renderer.New( renderer.WithParameters(params), renderer.WithExtraFunctions(), ).Render(tmpl) if err != nil { fmt.Println(err) } fmt.Println(result) }
Output: Hello World!
func MergeFunctions ¶ added in v0.0.6
MergeFunctions merges two template.FuncMap instances, overrides if necessary
func N ¶ added in v0.1.5
N returns a slice of integers form the given start to end (inclusive)
Example (Empty) ¶
package main import ( "fmt" "github.com/VirtusLab/render/renderer" ) func main() { tmpl := ` {{ range $i := n 0 0 }}{{ $i }} {{ end }} ` result, err := renderer.New( renderer.WithExtraFunctions(), ).Render(tmpl) if err != nil { fmt.Println(err) } fmt.Println(result) }
Output: 0
Example (Simple) ¶
package main import ( "fmt" "github.com/VirtusLab/render/renderer" ) func main() { tmpl := ` {{ range $i := n 0 10 }}{{ $i }} {{ end }} ` result, err := renderer.New( renderer.WithExtraFunctions(), ).Render(tmpl) if err != nil { fmt.Println(err) } fmt.Println(result) }
Output: 0 1 2 3 4 5 6 7 8 9 10
func ToYAML ¶ added in v0.1.4
ToYAML is a template function, it turns a marshallable structure into a YAML fragment
func WithCryptFunctions ¶ added in v0.0.6
WithCryptFunctions mutates Renderer configuration by merging the Crypt template functions
func WithDelim ¶ added in v0.0.6
WithDelim mutates Renderer configuration by replacing the left and right delimiters
func WithExtraFunctions ¶ added in v0.0.6
WithExtraFunctions mutates Renderer configuration by merging the custom template functions
func WithFunctions ¶ added in v0.0.6
WithFunctions mutates Renderer configuration by replacing the template functions
func WithMoreFunctions ¶ added in v0.0.6
WithMoreFunctions mutates Renderer configuration by merging the given template functions,
func WithMoreParameters ¶ added in v0.0.8
WithMoreParameters mutates Renderer configuration by merging the given template parameters
func WithOptions ¶ added in v0.0.6
WithOptions mutates Renderer configuration by replacing the template functions
func WithParameters ¶ added in v0.0.6
WithParameters mutates Renderer configuration by replacing all template parameters
func WithSprigFunctions ¶ added in v0.0.6
WithSprigFunctions mutates Renderer configuration by merging the Sprig template functions
Types ¶
type Renderer ¶
type Renderer interface { base.Renderer Clone(configurators ...func(*config.Config)) Renderer FileRender(inputPath, outputPath string) error DirRender(inputDir, outputDir string) error NestedRender(args ...interface{}) (string, error) ReadFile(file string) (string, error) }
Renderer allows for parameterised text template rendering
Directories ¶
Path | Synopsis |
---|---|
Package parameters defines data structure for the data-driven renderer Parameters is a tree structure and can be created from a YAML files or 'key=value' pairs.
|
Package parameters defines data structure for the data-driven renderer Parameters is a tree structure and can be created from a YAML files or 'key=value' pairs. |