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: 11 modules, 13 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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