Socket
Socket
Sign inDemoInstall

github.com/tyler-sommer/stick

Package Overview
Dependencies
1
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/tyler-sommer/stick

Package stick is a Go language port of the Twig templating engine. Stick executes Twig templates and allows users to define custom Functions, Filters, and Tests. The parser allows parse-time node inspection with NodeVisitors, and a template Loader to load named templates from any source. Stick itself is a parser and template executor. If you're looking for Twig compatibility, check out package https://pkg.go.dev/github.com/tyler-sommer/stick/twig For additional information on Twig, check http://twig.sensiolabs.org/ Obligatory "Hello, World!" example: Another example, using a FilesystemLoader and responding to an HTTP request: Any user value in Stick is represented by a stick.Value. There are three main types in Stick when it comes to built-in operations: strings, numbers, and booleans. Of note, numbers are represented by float64 as this matches regular Twig behavior most closely. Stick makes no restriction on what is stored in a stick.Value, but some built-in operators will try to coerce a value into a boolean, string, or number depending on the operation. Additionally, custom types that implement specific interfaces can be coerced. Stick defines three interfaces: Stringer, Number, and Boolean. Each interface defines a single method that should convert a custom type into the specified type. On a final note, there exists three functions to coerce any type into a string, number, or boolean, respectively. It is possible to define custom Filters, Functions, and boolean Tests available to your Stick templates. Each user-defined type is simply a function with a specific signature. A Func represents a user-defined function. Functions can be called anywhere expressions are allowed. Functions may take any number of arguments. A Filter is a user-defined filter. Filters receive a value and modify it in some way. Filters also accept zero or more arguments beyond the value to be filtered. A Test represents a user-defined boolean test. Tests are used to make some comparisons more expressive. Tests also accept zero to any number of arguments, and Test names can contain up to one space. User-defined types are added to an Env after it is created. For example:


Version published

Readme

Source

Stick

CircleCI GoDoc

A Go language port of the Twig templating engine.

Overview

This project is split over two main parts.

Package github.com/tyler-sommer/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/tyler-sommer/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.

  • text/template and html/template from the Go standard library.
  • pongo2 is a full-featured Go language port of Django's templating language.

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/tyler-sommer/stick

Usage

Execute a simple Stick template.

package main

import (
	"log"
	"os"
    
	"github.com/tyler-sommer/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

  • Autoescaping (see: Twig compatibility)
  • Whitespace control
  • Improve error reporting
Further

FAQs

Last updated on 01 Apr 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc