templatex

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2025 License: MIT Imports: 11 Imported by: 4

README

templateX

templateX is a go template engine with some extra features.

import "github.com/rytsh/mugo/templatex"

Usage

tpl := templatex.New(store.WithAddFuncsTpl(
	fstore.FuncMapTpl(
		fstore.WithLog(logz.AdapterKV{Log: log.Logger}),
		fstore.WithTrust(true),
		fstore.WithWorkDir("."),
	),
))

tpl.Execute(
	templatex.WithIO(output),
	templatex.WithData(inputData),
	templatex.WithParsed(true),
);

Documentation

Overview

Example
package main

import (
	"bytes"
	"fmt"
	"log"

	"github.com/rytsh/mugo/templatex"
)

func main() {
	tpl := templatex.New()

	tpl.AddFunc("add", func(a, b int) int {
		return a + b
	})

	tpl.AddFunc("sub", func(a, b int) int {
		return a - b
	})

	var output bytes.Buffer
	if err := tpl.Execute(
		templatex.WithIO(&output),
		templatex.WithData(map[string]interface{}{
			"a": 1,
			"b": 2,
		}),
		templatex.WithContent(`a + b = {{ add .a .b }}`+"\n"+`a - b = {{ sub .a .b }}`),
	); err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s", output.String())
}
Output:

a + b = 3
a - b = -1

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultTemplateName = "_templatex"

Functions

This section is empty.

Types

type Info

type Info struct {
	// Name of the function
	Name string
	// Description of the function
	Description string
	// Usage of the function
	Usage string
}

type OptionExecute

type OptionExecute func(options *options)

OptionExecute to execute the template.

func WithContent

func WithContent(content string) OptionExecute

WithContent sets the content to parse, if WithParsed used this option is ignored.

func WithData

func WithData(values any) OptionExecute

WithData sets the data to use in Execute* functions. This is the values passed to the template.

func WithIO

func WithIO(w io.Writer) OptionExecute

WithIO sets the writer to use. Useful for Execute function.

func WithParsed

func WithParsed(parsed bool) OptionExecute

WithParsed sets the parsed template to use in Execute* functions.

func WithTemplate

func WithTemplate(template string) OptionExecute

WithTemplate sets the specific template to execute.

type OptionTemplate

type OptionTemplate func(*optionsTemplate)

func WithAddFunc

func WithAddFunc(key string, f interface{}) OptionTemplate

WithAddFunc for adding a function.

func WithAddFuncMap

func WithAddFuncMap(funcMap map[string]interface{}) OptionTemplate

WithAddFuncMap for adding multiple functions.

func WithAddFuncTpl

func WithAddFuncTpl[T any](key string, f func(T) interface{}) OptionTemplate

WithAddFuncTpl for adding a function. The function is execute with the value passed to WithFnValue.

func WithAddFuncsTpl

func WithAddFuncsTpl[T any](fn func(T) map[string]interface{}) OptionTemplate

WithAddFuncsTpl for adding multiple functions. The function is execute with the value passed to WithFnValue.

func WithFnValue

func WithFnValue[T any](fn T) OptionTemplate

WithFnValue for passing a value to the function for WithAddFuncsTpl and WithAddFuncTpl. In templatex, default value is templatex.

func WithHTMLTemplate

func WithHTMLTemplate() OptionTemplate

type SubFuncs

type SubFuncs struct {
	Name     string
	Type     reflect.Type
	Inner    uint8
	TypeName string
	First    bool
	End      uint8
}

type Template

type Template struct {
	// contains filtered or unexported fields
}

func New

func New(opts ...OptionTemplate) *Template

New returns a new Template.

func (*Template) AddFunc

func (t *Template) AddFunc(name string, fn interface{})

AddFunc for adding a func to the template.

func (*Template) AddFuncMap

func (t *Template) AddFuncMap(funcMap map[string]any)

AddFuncMap for extra textTemplate functions.

func (*Template) Clone

func (t *Template) Clone() (*Template, error)

func (*Template) Execute

func (t *Template) Execute(opts ...OptionExecute) error

Execute the template and write the output to the buffer. Add WithIO to change the writer.

Example to execute a template with data and use parsed template:

t.Execute(WithTemplate(templateName), WithData(data), WithParsed(true))

Example to execute and return the result:

var buf bytes.Buffer
t.Execute(WithIO(&buf), WithData(data))

func (*Template) ExecuteTemplate

func (t *Template) ExecuteTemplate(wr io.Writer, name string, data any) error

func (*Template) FuncInfos

func (t *Template) FuncInfos() []Info

func (*Template) ListFuncs

func (t *Template) ListFuncs() []Info

ListFuncs returns the list of functions with name order.

func (*Template) Parse

func (t *Template) Parse(content string) error

Parse content and set new template to parsed.

func (*Template) ParseFS

func (t *Template) ParseFS(fsys fs.FS, patterns ...string) error

func (*Template) ParseGlob

func (t *Template) ParseGlob(pattern string) error

ParseGlob parses the template definitions in the files identified by the pattern.

func (*Template) Reset

func (t *Template) Reset()

Reset the template and add the functions back.

func (*Template) SetDelims

func (t *Template) SetDelims(left, right string) *Template

SetDelims sets the template delimiters to the specified strings and returns the template to allow chaining.

func (*Template) SetTypeHTML added in v0.8.1

func (t *Template) SetTypeHTML()

SetTypeHTML converts the template to html template.

This function will reset the template when switching from text to html template.

func (*Template) SetTypeText

func (t *Template) SetTypeText()

SetTypeText converts the template to text template.

This function will reset the template when switching from html to text template.

Jump to

Keyboard shortcuts

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