jmoiron / jigo

an attempt at a jinja2 implementation in go
MIT License
72 stars 10 forks source link

jigo

Build Status Godoc license version

Jigo is a Jinja2-like template language for Go. It is in pre-alpha stages and not yet ready for use much less production. Any documentation you see here is subject to change. Jigo's name is also subject to change.

Goals

The goal of jigo is to create a template system which is powerful, flexible, and familiar. The Django template syntax has inspired Jinja2, Twig in php, Liquid in Ruby, Jinja-JS in JavaScript, and indeed Pongo2 in Go.

Although jigo is an outright attempt to implement a very functional subset of Jinja2's semantics, the fact that it is written in Go means that much of Jinja2's support for rich Python expressions and semantics are dropped.

Twig, a Jinja2-like template system implemented in PHP, allows for a limited expression syntax which more closely mimics Python's than PHP's. Jigo allows for a similarly Python-inspired expression syntax, with a clean, explicit two way mapping to Go types and stronger type matching requirements.

Unlike Go's html/template, There are no plans and currently no support for context-aware escaping. Because of html/template, this is a big deal for many in the Go community, and while I'm not against adding it to Jigo, I am more interested in getting the language in a usable state first.

Differences

There are many differences. Some infrequently used features of Jinja2 have been dropped, and the expression syntax is far less sophisticated.

Features

TODO More info here as I get through more of the implementation.

Type Safety

Template lookups are obviously not inspectable for types at compile time, but literals are, and although there are no type declarations, all types are inferred according to rules defined below. This means that the following code raises a type error at compile time:

{{ 1 + "foo" }}

Expressions

Expressions are coerced to strings at render time with fmt.Sprint, but expression syntax is strongly typed. Unlike Go, integer and floating point arithmetic can be mixed, but any introduction of floats coerces all values in the expression to float.

Function calling semantics allow for keyword arguments if the function's final argument is of type jigo.Kwargs. Varargs are allowed if a funciton is variadic or the final argument is of type jigo.Args. For a function to be both variadic and keyword, it must accept (jigo.Args, jigo.Kwargs) in that order.

Jigo follows Go's operator precedence and Go's definition of %, which is remainder, like C, and unlike Python. % is only defined in integers, and will be a compile time error on float literals and a runtime error on float variables.

Other Operators:

Literals