template

package
v0.0.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package template contains functions for rendering templates.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RenderAll

func RenderAll(templatePath string, excludedPaths []string, outputPath string, values Scope, delimiters Delimiters) error

RenderAll renders all templates in the path (directory or a single file) to an output path (directory or file) using the provided value scope.

Types

type Delimiters added in v0.0.15

type Delimiters struct {
	Start string
	End   string
}

func ParseDelimiters added in v0.0.15

func ParseDelimiters(s string) (Delimiters, error)

type ErrPathEmpty added in v0.0.11

type ErrPathEmpty struct{}

func (ErrPathEmpty) Error added in v0.0.11

func (e ErrPathEmpty) Error() string

type ErrRenderFail added in v0.0.9

type ErrRenderFail struct {
}

func (ErrRenderFail) Error added in v0.0.9

func (e ErrRenderFail) Error() string

type ErrScopeMergeConflict

type ErrScopeMergeConflict struct {
	Path string
}

ErrScopeMergeConflict indicates that a conflict occurred when merging two scopes at the specified path.

func (ErrScopeMergeConflict) Error

func (e ErrScopeMergeConflict) Error() string

type Scope

type Scope map[interface{}]interface{}

Scope represents a single lexical value scope. Scopes can be nested.

func ParseScope

func ParseScope(v interface{}) (Scope, bool)

ParseScope casts the value to Scope.

func (Scope) AssocAt added in v0.0.11

func (scope Scope) AssocAt(path string, value interface{}, force bool) error

AssocAt modifies scope by setting value at the provided path. It will create scopes along the way if they don't exist. In case the value pointed by the path already exists and cannot be interpreted as a Scope (see ParseScope), the function signals ErrScopeMergeConflict unless force is true. If that's the case, the value is overridden.

func (Scope) Flat

func (scope Scope) Flat() Scope
Example
package main

import (
	"fmt"

	"github.com/tooploox/oya/pkg/template"
)

func main() {
	scope := template.Scope{
		"foo": map[interface{}]interface{}{
			"bar": "baz",
			"qux": []interface{}{
				"1", "2", 3,
			},
			"abc": map[string]interface{}{
				"123": true,
			},
		},
	}
	flattened := scope.Flat()
	fmt.Println("foo.bar:", flattened["foo.bar"])
	_, ok := flattened["foo"]
	fmt.Println("foo exists?", ok)
	fmt.Println("foo.qux.0:", flattened["foo.qux.0"])
	fmt.Println("foo.qux.1:", flattened["foo.qux.1"])
	fmt.Println("foo.qux.2:", flattened["foo.qux.2"])
	fmt.Println("foo.abc.123:", flattened["foo.abc.123"])
}
Output:

foo.bar: baz
foo exists? false
foo.qux.0: 1
foo.qux.1: 2
foo.qux.2: 3
foo.abc.123: true

func (Scope) GetScopeAt

func (scope Scope) GetScopeAt(path string) (Scope, error)

GetScopeAt returns scope at the specified path. If path doesn't exist or points to a value that cannot be interpreted as a Scope (see ParseScope), the function signals an error.

func (Scope) Merge

func (scope Scope) Merge(other Scope) Scope

Merge combines two scopes together. If a key appears in both scopes, the other scope wins.

func (Scope) Replace

func (scope Scope) Replace(other Scope)

Replace replaces contents of this scope with keys and values of the other one.

func (Scope) UpdateScopeAt

func (scope Scope) UpdateScopeAt(path string, f func(Scope) Scope, force bool) error

UpdateScopeAt transforms a scope pointed to by the path (e.g. "foo.bar.baz"). It will create scopes along the way if they don't exist. In case the value pointed by the path already exists and cannot be interpreted as a Scope (see ParseScope), the function signals ErrScopeMergeConflict unless force is true. If that's the case, the value is overridden.

type Template

type Template interface {
	Render(out io.Writer, values Scope) error
	RenderString(values Scope) (string, error)
}

Template represents a template that can be rendered using provided values.

func Load

func Load(path string, delimiters Delimiters) (Template, error)

Load loads template from the path.

func Parse

func Parse(source string, delimiters Delimiters) (Template, error)

Parse parses template in the source string.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL