Golang html/template versus Python Jinja2 (7) - Custom Functions and Filters


The custom function in Go html/template is similar to Jinja2 custom filters in terms of syntax and functionality. The custom function can also be used in Jinja2 template (see [d]). This post will compare custom functions in Go html/template with custom filters/functions in Jinja2.



Go html/template versue Python Jinja2 - Custom Functions and Filters
Go html/template Python Jinja2

custom function gettext

func gettext(s string) string {
        if s == "world" {
                return "世界"
        }
        return s
}

custom function/filter gettext

def gettext(s):
  if s == "world":
    return u"世界"
  return s

custom function in template:

<span>hello {{gettext .}}</span>

use custom function with syntax similar to Jinja2 filter:

<span>hello {{. | gettext}}</span>

custom function in Jinja2 template: (see [d])

<span>hello {{gettext(name)}}</span>

custom filter in Jinja2 template: (see [e] [f])

<span>hello {{name | gettext}}</span>

load custom function and output: (see [a])

var funcMap = template.FuncMap{
       "gettext": gettext,
}

t, _ := template.New("foo").Funcs(funcMap).Parse(tmpl)
s := "world"
t.Execute(os.Stdout, s)

load custom function and output:

t = Template(tmpl)
t.globals['gettext'] = gettext

sys.stdout.write(t.render(name="world"))

load custom filter and output:

env = Environment()
env.filters['gettext'] = gettext
t = env.from_string(tmpl)

sys.stdout.write(t.render(name="world"))

Complete Go html/template source code:

Custom Function used in template:

html-template-example-5.go | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// http://stackoverflow.com/questions/17843311/template-and-custom-function-panic-function-not-defined
package main

import (
	"html/template"
	"os"
)

const tmpl = `
<span>hello {{gettext .}}</span>
<span>hello {{. | gettext}}</span>
`

var funcMap = template.FuncMap{
	"gettext": gettext,
}

func gettext(s string) string {
	if s == "world" {
		return "世界"
	}
	return s
}

func main() {
	t, _ := template.New("foo").Funcs(funcMap).Parse(tmpl)
	s := "world"
	t.Execute(os.Stdout, s)
}

Complete Python Jinja2 source code:

Custom Function used in template:

jinja2-example-5.py | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python
# -*- coding:utf-8 -*-

from jinja2 import Template
import sys

tmpl = """
<span>hello {{gettext(name)}}</span>
"""

def gettext(s):
  if s == "world":
    return u"世界"
  return s

if __name__ == '__main__':
  t = Template(tmpl)
  t.globals['gettext'] = gettext

  sys.stdout.write(t.render(name="world"))

Custom Filter used in template:

jinja2-example-5_1.py | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python
# -*- coding:utf-8 -*-

from jinja2 import Environment
import sys

tmpl = """
<span>hello {{name | gettext}}</span>
"""

def gettext(s):
  if s == "world":
    return u"世界"
  return s

if __name__ == '__main__':
  env = Environment()
  env.filters['gettext'] = gettext
  t = env.from_string(tmpl)

  sys.stdout.write(t.render(name="world"))

Tested on: Ubuntu Linux 14.10, Go 1.4, Python 2.7.8, Jinja2 2.7.3


Golang html/template versus Python Jinja2 series:

[1]Golang html/template versus Python Jinja2 (1)
[2]Golang html/template versus Python Jinja2 (2)
[3]Golang html/template versus Python Jinja2 (3) - Arrays and Slices
[4]Golang html/template versus Python Jinja2 (4) - Arrays and Slices Index
[5]Golang html/template versus Python Jinja2 (5) - Maps and Dictionaries
[6]Golang html/template versus Python Jinja2 (6) - Template Inheritance (Extends)
[7]Golang html/template versus Python Jinja2 (7) - Custom Functions and Filters

References:

[a]go - Template and custom function; panic: function not defined - Stack Overflow
[b]TechnoSophos: Using Custom Template Functions in Go
[c]Google Search: go template function
[d](1, 2) Call a python function from jinja2 - Stack Overflow
[e]Custom Filters - API - Jinja2 Documentation
[f]google app engine - Adding a custom filter to jinja2 on GAE - Stack Overflow