gonja

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 12 Imported by: 0

README

gonja

gonja is a pure go implementation of the Jinja template engine. It aims to be as compatible as possible with the original python implementation.

Usage

As a library

Install or update using go get:

go get github.com/ardanlabs/gonja

Example

package main

import (
	"os"
	"fmt"

	"github.com/ardanlabs/gonja"
	"github.com/ardanlabs/gonja/exec"
)

func main() {
	template, err := gonja.FromString("Hello {{ name | capitalize }}!")
	if err != nil {
		panic(err)
	}

	data := exec.NewContext(map[string]interface{}{
		"name": "bob",
	})

	if err = template.Execute(os.Stdout, data); err != nil { // Prints: Hello Bob!
		panic(err)
	}
}

Documentation

Limitations

  • format: format does not take python's string format syntax as a parameter, instead it takes Go's. Essentially {{ 3.14|stringformat:"pi is %.2f" }} is fmt.Sprintf("pi is %.2f", 3.14)
  • escape / force_escape: Unlike Jinja's behavior, the escape-filter is applied immediately. Therefore there is no need for a force_escape filter
  • Only subsets of native python types (bool, int, float, str, dict and list) methods have been re-implemented in Go and can slightly differ from the original ones

Development

Guidelines

Please read through the contribution guidelines before diving into any work.

Requirements

Tests

The unit tests can be run using:

ginkgo run -p ./...

Tribute

A massive thank you to the original author @noirbizarre for doing the initial work in https://github.com/noirbizarre/gonja which this project was forked from and @NikolaLohinski who has been maintaining the project since.

Documentation

Overview

Package gonja implements a Jinja2-like template engine for Go.

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultLoader      = loaders.MustNewFileSystemLoader("")
	DefaultConfig      = config.New()
	DefaultContext     = exec.EmptyContext().Update(builtins.GlobalFunctions).Update(builtins.GlobalVariables)
	DefaultEnvironment = &exec.Environment{
		Context:           DefaultContext,
		Filters:           builtins.Filters,
		Tests:             builtins.Tests,
		ControlStructures: builtins.ControlStructures,
		Methods:           builtins.Methods,
	}
)
View Source
var NewConfig = config.New

Functions

func FromBytes

func FromBytes(source []byte) (*exec.Template, error)

func FromFile

func FromFile(filepath string) (*exec.Template, error)

func FromString

func FromString(source string) (*exec.Template, error)

func SetLoggerLevel

func SetLoggerLevel(level logrus.Level)

func SetLoggerOutput

func SetLoggerOutput(out io.Writer)

Types

type Config

type Config config.Config

Directories

Path Synopsis
Package builtins provides built-in filters, tests, control structures, and global functions.
Package builtins provides built-in filters, tests, control structures, and global functions.
control_structures
Package controlstructures provides built-in control structure implementations.
Package controlstructures provides built-in control structure implementations.
methods
Package methods provides built-in method implementations for template types.
Package methods provides built-in method implementations for template types.
methods/pybool
Package pybool provides Python-compatible boolean methods.
Package pybool provides Python-compatible boolean methods.
methods/pyerrors
Package pyerrors provides Python-compatible error types.
Package pyerrors provides Python-compatible error types.
methods/pyfloat
Package pyfloat provides Python-compatible float methods.
Package pyfloat provides Python-compatible float methods.
methods/pyint
Package pyint provides Python-compatible integer methods.
Package pyint provides Python-compatible integer methods.
methods/pystring
Package pystring provides Python-compatible string methods.
Package pystring provides Python-compatible string methods.
Package config provides configuration types for the template engine.
Package config provides configuration types for the template engine.
Package docs provides embedded documentation assets.
Package docs provides embedded documentation assets.
examples
configuration command
custom_filter command
Package exec provides template execution support.
Package exec provides template execution support.
Package loaders provides template loading implementations.
Package loaders provides template loading implementations.
Package logging provides a flag to control whether logrus logging is enabled.
Package logging provides a flag to control whether logrus logging is enabled.
Package nodes provides AST node types for the template parser.
Package nodes provides AST node types for the template parser.
Package parser provides the template parser implementation.
Package parser provides the template parser implementation.
Package tokens provides lexical token types and the lexer.
Package tokens provides lexical token types and the lexer.
Package utils provides utility functions for template processing.
Package utils provides utility functions for template processing.

Jump to

Keyboard shortcuts

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