pongo2 v2 released
I'm happy to release the first major update for pongo2: v2. It got a lot of new features and bug fixes. Thanks to all reporters and contributors.
This release is backwards-incompatible to v1 which means chances are you have to adapt your codebase (slightly) in order to use v2. The pongo2 playground already uses pongo v2 (including the new features like sandboxing) and now got the official pongo2-addons package enabled for testing and playing with pongo2. pongo2-addons is already ready to use with pongo v2.
To install v2 and get backwards-compatible changes and bugfixes for later versions of the v2 series, use this import path:
Use this import path to stick with the latest development version of pongo2:
New major features
- Template sets: pongo2 allows to group similar kind of templates using a technique called template sets (for example web vs. email templates). Users can apply different configurations and sandbox restrictions (see below) on a specific template set. A default template set is created when pongo2 is imported (this is the one all pongo2-global API functions like
From*are working on); see theDefaultSethere. Features of the template sets include:- Debugging: If
Debugis set to true, logging of specific errors occur tostdout(you can print out errors as well using the newExecutionContext.Logffunction which will only print out your logging information whenDebugis true).Debughas implications on the new template caching function as well (see below). - Globals: It is now possible to provide a global context (in addition to a per-template context) for a template set. It's always possible to override global variables through a per-template context.
- Base directory: The previous behavior of a filename lookup (inside templates, for example when using the
include,extendsorssi-tag, or outside templates when calling theFrom*()-functions) was, in case of a relative path, to lookup the filename relative to the application's directory or, in case of an absolute path, to take this absolute one. It's now possible to change the base lookup directory (for relative paths) by using the new template set functionsSetBaseDirectory()andBaseDirectory(). - Sandboxing: To provide more security, pongo2 introduces a per-template-set sandbox. It can prohibit the usage of filters and tags or restrict file accesses (within templates when using a file-sensitive tag/filter like
includeor outside of templates when using a function likeFrom*()). Use the attributeSandboxDirectoriesto provide path patterns supported by http://golang.org/pkg/path/filepath/#Match in order to restrict the access to specifc files or directories. You can limit the tag/filter access by using the two new template set functionsBanFilterandBanTag.
- Debugging: If
- Template caching: pongo2 introduces a new API function
FromCachewhich behaves likeFromFile, but includes caching:FromCacheonly compiles a template once per template (even if called multiple times) and can be called from multiple Goroutines (it's thread-safe). When debugging is activated (see above), FromCache will re-compile the given template on any request (simplifies development to see live changes). - Macro imports/exports: pongo2 supports now importing and exporting of macros. Exporting is done by appending the
exportkeyword to your macro declaration (like this:{% macro my_exported_macro(args1, args2="default string") export %}). You can import exported macros by using the newimporttag:{% import "my_file_with_macros.html" my_exported_macro, another_exported_macro as renamed_macro %}. Macros import macros (chaining macros) is supported. - New pongo2-specific tag
set: Sets a variable to a given expression like this:{% set my_variable = 5 + 10 / another_var_number %}
New minor features
- Added support for the
reversedargument for thefor-tag elif-tag added (to support multiple cases when using theif-tag)- It's now possible to replace the behavior of already registered tags and filters through 2 new API-functions:
ReplaceTagandReplaceFilter. - It's now possible to mark a
pongo2.Valueassafe(so theescapefilter won't get applied when outputting the value) using the new API-functionAsSafeValue. This reduces the necessary to apply thesafefilter manually, for example when using themarkdownfilter from thepongo2-addonspackage. - New
Errortype which returns detailed machine-readable information (including the affected raw template line) about the error occurred and is now much more specific in some cases on which error occurred
Bugfixes and other improvements
- Better UTF-8 handling of filters and tags
- Performance of template execution improved
- Bugfix in handling of the
parsedargument of thessi-tag - Bugfix related to the operator associativity
- Better documentation (but still not perfect)
Notable backward-incompatible API-changes (v1 <-> v2):
- The interface type
INodeEvavluatorgot replaced byIEvaluator - Function signature for tag and filter parsing/execution changed (error return type changed to
*Error).
See the whole changelog here: v1...v2
As always, I'm happy about any feedback!