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 ¶
Types ¶
type AutoEscapeExtension ¶
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><script>bad stuff</script>
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><script>bad script</script> <script>good script</script>
func NewAutoEscapeExtension ¶
func NewAutoEscapeExtension() *AutoEscapeExtension
NewAutoEscapeExtension returns an AutoEscapeExtension with Twig equivalent Escapers, by default.