twig

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2024 License: MIT Imports: 5 Imported by: 0

README

Twig

Build Status GoDoc

Provides Twig-compatibility for the stick templating engine.

Overview

This is the Twig compatibility subpackage for Stick.

Current status
In development

Package bitbucket.org/ticketshopdev/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.

Package bitbucket.org/ticketshopdev/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.

Installation

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

go get -u bitbucket.org/ticketshopdev/stick/twig

Usage

Execute a simple Twig template.

package main

import (
	"log"
	"os"
	
	"bitbucket.org/ticketshopdev/stick"
	"bitbucket.org/ticketshopdev/stick/twig"
)

func main() {
    env := twig.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.

Documentation

Overview

Package twig provides Twig 1.x compatible template parsing and executing.

Note: This package is still in development.

Twig is a powerful templating language that promotes separation of logic from the view. This package attempts to replicate the functionality of the Twig engine using the Go programming language.

Twig provides an extensive list of built-in functions, filters, tests, and even auto-escaping. This package provides this functionality out of the box, aiming to fully support the Twig spec.

A simple example might look like:

env := twig.New(nil);	// A nil loader means stick will execute
				// the string passed into env.Execute.

// Templates receive a map of string to any value.
p := map[string]stick.Value{"name": "World"}

// Substitute os.Stdout with any io.Writer.
env.Execute("Hello, {{ name }}!", os.Stdout, p)

Check the main package https://godoc.org/bitbucket.org/ticketshopdev/stick for more information on general functionality and usage.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(loader stick.Loader) *stick.Env

New creates a new, default Env that aims to be compatible with Twig. If nil is passed as loader, a StringLoader is used.

Types

type AutoEscapeExtension

type AutoEscapeExtension struct {
	Escapers map[string]Escaper
}

AutoEscapeExtension provides Twig equivalent escaping for Stick templates.

Example

This example shows how the AutoEscapeVisitor can be used to automatically sanitize input. It does this by wrapping printed expressions with a filter application, which resolves to stick.EscapeFilter.

package main

import (
	"os"

	"bitbucket.org/ticketshopdev/stick"
	"bitbucket.org/ticketshopdev/stick/twig"
)

func main() {
	env := twig.New(nil)
	env.Execute("<html>{{ '<script>bad stuff</script>' }}", os.Stdout, map[string]stick.Value{})
}
Output:

<html>&lt;script&gt;bad stuff&lt;/script&gt;
Example (AlreadySafe)

This example displays the EscapeFilter in action.

Note the "already_safe" value wrapped in a NewSafeValue; it is not escaped.

package main

import (
	"os"

	"bitbucket.org/ticketshopdev/stick"
	"bitbucket.org/ticketshopdev/stick/twig"
)

func main() {
	env := twig.New(nil)
	env.Execute("<html>{{ dangerous|escape }} {{ already_safe|escape }}", os.Stdout, map[string]stick.Value{
		"already_safe": stick.NewSafeValue("<script>good script</script>", "html"),
		"dangerous":    "<script>bad script</script>",
	})
}
Output:

<html>&lt;script&gt;bad script&lt;/script&gt; <script>good script</script>

func NewAutoEscapeExtension

func NewAutoEscapeExtension() *AutoEscapeExtension

NewAutoEscapeExtension returns an AutoEscapeExtension with Twig equivalent Escapers, by default.

func (*AutoEscapeExtension) Init

func (e *AutoEscapeExtension) Init(env *stick.Env) error

Init registers the escape functionality with the given Env.

type Escaper

type Escaper func(string) string

An Escaper returns the escaped input. Escapers should expect to receive unescaped input.

Directories

Path Synopsis
Package escape provides Twig-compatible escape functions.
Package escape provides Twig-compatible escape functions.
Package filter provides built-in filters for Twig-compatibility.
Package filter provides built-in filters for Twig-compatibility.

Jump to

Keyboard shortcuts

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