byte_template

package module
v0.0.0-...-69252eb Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2023 License: MIT Imports: 2 Imported by: 12

README

byte-template

Byte Template is a templating engine for byte slices with custom directives.

package main
import (
    "bytes"
    "fmt"
    "github.com/jensneuse/byte-template"
    "io"
)

func main() {
    template := byte_template.New(byte_template.DirectiveDefinition{
        Name:[]byte("toLower"),
        Resolve:func(w io.Writer, arg []byte) (n int,err error) {
            return w.Write(bytes.ToLower(arg))
        },
    })
    buf := bytes.Buffer{}
    _,_ = template.Execute(&buf,[]byte("/api/user/{{ toLower .name }}"),func(w io.Writer, path []byte) (n int,err error) {
        if bytes.Equal(path,[]byte("name")){
            _,err = w.Write([]byte("Jens"))
        }
        return      
    })
    fmt.Println(buf.String()) // Output: jens
}

Contributors

Documentation

Overview

byte_templating is intended to offer a simple templating engine for byte slices it's different from other implementations in two ways 1. you can define the "item" selector on your own, that is you don't have to supply a interface{} object, similar to https://github.com/valyala/fasttemplate 2. in addition to fasttemplate you can also define custom directives like "toLower", "URLEncode" etc. This library doesn't push any gc pressure to the user This library is not thread safe, use it with a sync.Pool

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DirectiveDefinition

type DirectiveDefinition struct {
	Name    []byte
	Resolve func(w io.Writer, arg []byte) (n int, err error)
}

type Fetch

type Fetch func(w io.Writer, path []byte) (n int, err error)

type Template

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

func New

func New(directiveDefinitions ...DirectiveDefinition) *Template

func (*Template) Execute

func (t *Template) Execute(w io.Writer, input []byte, fetch Fetch) (n int, err error)

Jump to

Keyboard shortcuts

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