pongo

package module
v0.0.0-...-9f6442c Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2014 License: MIT Imports: 11 Imported by: 5

README

GoDoc Build Status

I deactivated the issue tracker and wiki pages for pongo. pongo won't receive bugfixes anymore. Please consider using the successor pongo2 instead. You can find more information and a migration tutorial on my website.

pongo is a well-tested template engine which implements a Django-template-like syntax.

Please have a look at the test (template_test.go) for examples.

A tiny example (template string)

in := "Hello {{ name|capitalize }}!"
tpl, err := pongo.FromString("mytemplatetest", &in, nil)
if err != nil {
	panic(err)
}
out, err := tpl.Execute(&pongo.Context{"name": "florian"})
if err != nil {
	panic(err)
}
fmt.Println(*out) // Output: Hello Florian!

Example server-usage (template file)

package main

import (
	"github.com/flosch/pongo"
	"net/http"
)

var tplExample = pongo.Must(pongo.FromFile("example.html", nil))

func examplePage(w http.ResponseWriter, r *http.Request) {
	err := tplExample.ExecuteRW(w, &pongo.Context{"query": r.FormValue("query")})
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

func main() {
	http.HandleFunc("/", examplePage)
	http.ListenAndServe(":8080", nil)
}

Documentation

See the wiki (work in progress) on GitHub for a documentation/reference:

https://github.com/flosch/pongo/wiki.

While I'm working on the wiki content, GoPkgDoc shows a list of implemented filters/tags and an auto-generated documentation on how to use the simple API:

http://go.pkgdoc.org/github.com/flosch/pongo

It is possible to add your own filters/tags. See the template_test.go for example implementations.

Status

pongo is still in beta and has a very few known bugs (this is why the tests fail).

License

pongo is licensed under the MIT-license (see LICENSE file for more).

Documentation

Overview

Pongo implements a Django-syntax-like templating engine.

See the tests for examples.

Index

Constants

This section is empty.

Variables

View Source
var Filters = map[string]FilterFunc{
	"safe":        filterSafe,
	"unsafe":      nil,
	"lower":       filterLower,
	"upper":       filterUpper,
	"capitalize":  filterCapitalize,
	"default":     filterDefault,
	"trim":        filterTrim,
	"length":      filterLength,
	"join":        filterJoin,
	"striptags":   filterStriptags,
	"time_format": filterTimeFormat,
	"floatformat": filterFloatFormat,
}
View Source
var Tags = map[string]*TagHandler{
	"if":        &TagHandler{Execute: tagIf, Ignore: tagIfIgnore},
	"else":      nil,
	"endif":     nil,
	"for":       &TagHandler{Execute: tagFor, Ignore: tagForIgnore},
	"endfor":    nil,
	"block":     &TagHandler{Execute: tagBlock},
	"endblock":  nil,
	"extends":   &TagHandler{},
	"include":   &TagHandler{},
	"trim":      &TagHandler{Execute: tagTrim, Ignore: tagTrimIgnore},
	"endtrim":   nil,
	"remove":    &TagHandler{Execute: tagRemove, Ignore: tagRemoveIgnore},
	"endremove": nil,
}

Functions

This section is empty.

Types

type Context

type Context map[string]interface{}

A Context is used to pass data to the template. You can pass whatever you want in interface{}.

type FilterChainContext

type FilterChainContext struct {
	// Store what you want along the filter chain. Every filter has access to this store.
	Store map[string]interface{}
	// contains filtered or unexported fields
}

func (*FilterChainContext) HasVisited

func (ctx *FilterChainContext) HasVisited(names ...string) bool

type FilterFunc

type FilterFunc func(interface{}, []interface{}, *FilterChainContext) (interface{}, error)

type TagHandler

type TagHandler struct {
	Execute func(*string, *executionContext, *Context) (*string, error)
	Ignore  func(*string, *executionContext) error
	Prepare func(*tagNode, *Template) error
}

type Template

type Template struct {
	// contains filtered or unexported fields
}

func FromFile

func FromFile(file_path string, locator templateLocator) (*Template, error)

Reads a template from file. If there's no templateLocator provided, one will be created to search for files in the same directory the template file is located. file_path can either be an absolute filepath or a relative one.

func FromString

func FromString(name string, tplstr *string, locator templateLocator) (*Template, error)

Creates a new template instance from string.

func Must

func Must(t *Template, err error) *Template

The Must function is a little helper to create a template instance from string/file. It checks whether FromString/FromFile returns an error; if so, it panics. If not, it returns the template instance. Is's primarily used like this:

var tplExample = template.Must(template.FromFile("example.html", nil))

func (*Template) Execute

func (tpl *Template) Execute(ctx *Context) (out *string, err error)

Executes the template with the given context (can be nil).

func (*Template) ExecuteRW

func (tpl *Template) ExecuteRW(w http.ResponseWriter, ctx *Context) error

Executes the template with the given context and writes to http.ResponseWriter on success. Context can be nil. Nothing is written on error; instead the error is being returned.

func (*Template) SetDebug

func (tpl *Template) SetDebug(d bool)

pongo will print out a stacktrace whenever it panics if set to true.

Jump to

Keyboard shortcuts

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