jinja2

package module
v0.0.0-...-87ae351 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: Apache-2.0 Imports: 21 Imported by: 10

README

Embedded Jinja2 Renderer

This library provides an embedded Jinja2 renderer, based on https://github.com/kluctl/go-embed-python. It works by spawning one (or multiple) Jinaj2 renderer processes which communicate with your Go application via stdin/stdout.

Whenever something needs to be rendered, it is sent to the renderer, which will then process it. The result is returned to the caller.

Here is an example:

package main

import (
	"fmt"
	"github.com/kluctl/go-jinja2"
)

func main() {
	j2, err := jinja2.NewJinja2("example", 1,
		jinja2.WithGlobal("test_var1", 1),
		jinja2.WithGlobal("test_var2", map[string]any{"test": 2}))
	if err != nil {
		panic(err)
	}
	defer j2.Close()

	template := "{{ test_var1 }}"

	s, err := j2.RenderString(template)
	if err != nil {
		panic(err)
	}

	fmt.Printf("template: %s\nresult: %s", template, s)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Jinja2

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

func NewJinja2

func NewJinja2(name string, parallelism int, opts ...Jinja2Opt) (*Jinja2, error)

func (*Jinja2) Cleanup

func (j *Jinja2) Cleanup()

func (*Jinja2) Close

func (j *Jinja2) Close()

func (*Jinja2) RenderDirectory

func (j *Jinja2) RenderDirectory(sourceDir string, targetDir string, excludePatterns []string, opts ...Jinja2Opt) error

func (*Jinja2) RenderFile

func (j *Jinja2) RenderFile(template string, opts ...Jinja2Opt) (string, error)

func (*Jinja2) RenderFiles

func (j *Jinja2) RenderFiles(jobs []*RenderJob, opts ...Jinja2Opt) error

func (*Jinja2) RenderString

func (j *Jinja2) RenderString(template string, opts ...Jinja2Opt) (string, error)

func (*Jinja2) RenderStrings

func (j *Jinja2) RenderStrings(jobs []*RenderJob, opts ...Jinja2Opt) error

func (*Jinja2) RenderStruct

func (j *Jinja2) RenderStruct(o interface{}, opts ...Jinja2Opt) (bool, error)

type Jinja2Error

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

func (*Jinja2Error) Error

func (m *Jinja2Error) Error() string

type Jinja2Opt

type Jinja2Opt func(o *jinja2Options)

func WithDebugTrace

func WithDebugTrace(debugTrace bool) Jinja2Opt

func WithEmbeddedExtractDir

func WithEmbeddedExtractDir(p string) Jinja2Opt

func WithExtension

func WithExtension(e string) Jinja2Opt

func WithFilter

func WithFilter(name string, code string) Jinja2Opt

WithFilter adds a custom filter with `name` to the engine

name: the name of the filter code: the code defines a filter function

By default, name of the defined function should be same as the filter name. You can change this behaviour to set your filter name to 'xxx:yyy' format, then the real filter name is 'xxx' and the function name is 'yyy'.

For example, you can use

WithFilter("add", "def add(x, y): return x + y")

And also, you can use

WithFilter("add:my_add", "def my_add(x, y): return x + y")

func WithGlobal

func WithGlobal(k string, v any) Jinja2Opt

func WithGlobals

func WithGlobals(globals map[string]any) Jinja2Opt

func WithLStripBlocks

func WithLStripBlocks(lstripBlocks bool) Jinja2Opt

func WithPythonPath

func WithPythonPath(p string) Jinja2Opt

func WithSearchDir

func WithSearchDir(dir string) Jinja2Opt

func WithSearchDirs

func WithSearchDirs(dirs []string) Jinja2Opt

func WithStrict

func WithStrict(strict bool) Jinja2Opt

func WithTemplateIgnoreRootDir

func WithTemplateIgnoreRootDir(dir string) Jinja2Opt

func WithTraceJsonReceive

func WithTraceJsonReceive(f func(map[string]any)) Jinja2Opt

func WithTraceJsonSend

func WithTraceJsonSend(f func(map[string]any)) Jinja2Opt

func WithTrimBlocks

func WithTrimBlocks(trimBlocks bool) Jinja2Opt

type RenderJob

type RenderJob struct {
	Template string
	Result   *string
	Error    error
	// contains filtered or unexported fields
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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