Categorygithub.com/monstrum/stick
modulepackage
1.1.0
Repository: https://github.com/monstrum/stick.git
Documentation: pkg.go.dev

# README

Stick

CircleCI GoDoc

A Go language port of the Twig templating engine.

Overview

This project is split across two parts.

Package github.com/monstrum/stick is a Twig template parser and executor. It provides the core functionality and offers many of the same extension points as Twig like functions, filters, node visitors, etc.

Package github.com/monstrum/stick/twig contains extensions to provide the most Twig-like experience for template writers. It aims to feature the same functions, filters, etc. to be closely Twig-compatible.

Current status

Stable, mostly feature complete

Stick itself is mostly feature-complete, with the exception of whitespace control, and better error handling in places.

Stick is made up of three main parts: a lexer, a parser, and a template executor. Stick's lexer and parser are complete. Template execution is under development, but essentially complete.

See the to do list for additional information.

Alternatives

These alternatives are worth checking out if you're considering using Stick.

Installation

Stick is intended to be used as a library. The recommended way to install the library is using go get.

go get -u github.com/monstrum/stick

Usage

Execute a simple Stick template.

package main

import (
	"log"
	"os"
    
	"github.com/monstrum/stick"
)

func main() {
	env := stick.New(nil)
	if err := env.Execute("Hello, {{ name }}!", os.Stdout, map[string]stick.Value{"name": "Tyler"}); err != nil {
		log.Fatal(err)
	}
}

See godoc for more information.

To do

Further

# Packages

Package parse handles transforming Stick source code into AST for further processing.
Package twig provides Twig 1.x compatible template parsing and executing.

# Functions

CoerceBool coerces the given value into a boolean.
CoerceNumber coerces the given value into a number.
CoerceString coerces the given value into a string.
Contains returns true if the haystack Value contains needle.
Equal returns true if the two Values are considered equal.
GetAttr attempts to access the given value and return the specified attribute.
IsArray returns true if the given Value is a slice or array.
IsIterable returns true if the given Value is a slice, array, or map.
IsMap returns true if the given Value is a map.
Iterate calls the Iteratee func for every item in the Value.
Len returns the Length of Value.
New creates an empty Env.
NewFilesystemLoader creates a new FilesystemLoader with the specified root directory.
NewSafeValue wraps the given value and returns a SafeValue.

# Structs

Env represents a configured Stick environment.
A FilesystemLoader loads templates from a filesystem.
Loop contains metadata about the current state of a loop.
MemoryLoader loads templates from an in-memory map.
StringLoader is intended to be used to load Stick templates directly from a string.

# Interfaces

Boolean is implemented by any value that has a Boolean method.
A Context represents the execution context of a template.
ContextMetadata contains additional, unstructured runtime attributes about the template being executed.
ContextScope provides an interface with the currently executing template's scope.
An Extension is used to group related functions, filters, visitors, etc.
Loader defines a type that can load Stick templates using the given name.
Number is implemented by any value that has a Number method.
A SafeValue represents a value that has already been sanitized and escaped.
Stringer is implemented by any value that has a String method.
A Template represents a named template and its contents.
A Value represents some value, scalar or otherwise, able to be passed into and used by a Stick template.

# Type aliases

A Filter is a user-defined filter.
A Func represents a user-defined function.
An Iteratee is called for each step in a loop.
A Test represents a user-defined test.