发布于 2014-10-04 22:55:02 | 316 次阅读 | 评论: 0 | 来源: 网友投递
pongo2 Go模板引擎
pongo2 是一个语法与 Django 模板类似的 Go 语言模板引擎,并且完全兼容 Django 模板。pongo 2提供了复杂和嵌套函数调用和强大的类C表达式。
pongo2 v2 发布了,pongo 2 是一个 Go 语言的模板引擎,其语法与 Django 模板类似,并且完全兼容 Django 模板。
新版本包含大量新特性和 bug 修复。但是该版本不兼容 v1 。可使用 pongo2-addons
进行 pongo2 的测试。
pongo2 主要改进包括:
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 the DefaultSet
here. Features of the template sets include:
Debugging: If Debug
is set to true, logging of specific errors occur to stdout
(you can print out errors as well using the new ExecutionContext.Logf
function which will only print out your logging information when Debug
is true). Debug
has 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
, extends
or ssi
-tag, or outside templates when calling the From*()
-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 functions SetBaseDirectory()
and BaseDirectory()
.
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 include
or outside of templates when using a function like From*()
). Use the attribute SandboxDirectories
to 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 functions BanFilter
and BanTag
.
Template caching: pongo2 introduces a new API function FromCache
which behaves like FromFile
, but includes caching: FromCache
only 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 export
keyword to your macro declaration (like this: {% macro my_exported_macro(args1, args2="default string") export %}
). You can import exported macros by using the new import
tag: {% 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 %}
Added support for the reversed
argument for the for
-tag
elif
-tag added (to support multiple cases when using the if
-tag)
It’s now possible to replace the behavior of already registered tags and filters through 2 new API-functions: ReplaceTag
and ReplaceFilter
.
It’s not possible to mark a pongo2.Value
as safe
(so the escape
filter won’t get applied when outputting the value) using the new API-function AsSafeValue
. This reduces the necessary to apply the safe
filter manually, for example when using the markdown
filter from the pongo2-addons
package.
New Error
type 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
Better UTF-8 handling of filters and tags
Performance of template execution improved
Bugfix in handling of the parsed
argument of the ssi
-tag
Bugfix related to the operator associativity
Better documentation (but still not perfect)
Other minor bugfixes
The interface type INodeEvavluator
got replaced by IEvaluator
Function signature for tag and filter parsing/execution changed (error return type changed to *Error
).
完整记录请看: https://github.com/flosch/pongo2/compare/v1...v2