

Golang html/template versus Python Jinja2 (5) - Maps and Dictionaries
source link: http://siongui.github.io/2015/03/07/python-jinja2-vs-go-html-template-map-dictionary/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Golang html/template versus Python Jinja2 (5) - Maps and Dictionaries
March 07, 2015
This post shows how to loop through the maps in Go html/template and dictionaries in Python Jinja2.
In Jinja2, loop.index or loop.index0 is used to access the loop index, starting from 1 or 0. (see [a])
In Go html/template, it seems that there is no way to access the loop index while loop through the variable of map type. (see [b])
Go html/template versue Python Jinja2 - Maps and Dictionaries
Go html/template Python Jinja2
template:
{{range $name, $href := .}} <a href="{{$href}}">{{$name}}</a> {{end}}
template:
{% for name, href in links.iteritems() %} <a href="{{ href }}">{{ name }}</a> {% endfor %}
loop index starting from 1:
{% for name, href in links.iteritems() %} {{ loop.index }}: <a href="{{ href }}">{{ name }}</a> {% endfor %}
loop index starting from 0:
{% for name, href in links.iteritems() %} {{ loop.index0 }}: <a href="{{ href }}">{{ name }}</a> {% endfor %}
template values:
var m = map[string]string{ "Google": "https://www.google.com/", "Facebook": "https://www.facebook.com/", }
template values:
links = { 'Google': 'https://www.google.com', 'Facebook': 'https://www.facebook.com', }
Complete Go html/template source code:
html-template-example-4.go | repository | view raw
package main import ( "html/template" "os" ) const tmpl = ` {{range $name, $href := .}} <a href="{{$href}}">{{$name}}</a> {{end}} ` func main() { // map var m = map[string]string{ "Google": "https://www.google.com/", "Facebook": "https://www.facebook.com/", } t, _ := template.New("foo").Parse(tmpl) t.Execute(os.Stdout, m) }
Complete Python Jinja2 source code:
jinja2-example-4.py | repository | view raw
#!/usr/bin/env python # -*- coding:utf-8 -*- from jinja2 import Template import sys tmpl = """ {% for name, href in links.iteritems() %} <a href="{{ href }}">{{ name }}</a> {% endfor %} {% for name, href in links.iteritems() %} {{ loop.index }}: <a href="{{ href }}">{{ name }}</a> {% endfor %} {% for name, href in links.iteritems() %} {{ loop.index0 }}: <a href="{{ href }}">{{ name }}</a> {% endfor %} """ if __name__ == '__main__': links = { 'Google': 'https://www.google.com', 'Facebook': 'https://www.facebook.com', } t = Template(tmpl) sys.stdout.write(t.render(links=links))
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:
Recommend
-
111
Jinja Jinja is a fast, expressive, extensible templating engine. Special placeholders in the template allow writing code similar to Python syntax. Then the template is passed data to render the final document. It includes: ...
-
12
Uber 遠端代碼執行- Uber.com Remote Code Execution via Flask Jinja2 Template Injection 好久沒 po 文了XD幾天前,Uber 公佈了 Bug Bounty 計畫,從
-
6
Reading Time: 3 minutesJinja2 is a modern and design friendly templating engine for Python programming language. Jinja2 is a library for Python that is flexible, fast and secure.Jinja can generate any text-based format (HTML, XML, CSV, JSON,...
-
13
Introduction This posts will show how to do
-
8
Golang html/template versus Python Jinja2 (7) - Custom Functions and Filters ...
-
6
Golang html/template versus Python Jinja2 (2) February 24, 2015 Print He...
-
10
Golang html/template versus Python Jinja2 (6) - Template Inheritance (Extends) March 08...
-
5
Golang html/template versus Python Jinja2 (1) Updated: March 06, 2015 Th...
-
8
Golang html/template versus Python Jinja2 (4) - Arrays and Slices Index March 06, 2015...
-
15
Golang html/template versus Python Jinja2 (3) - Arrays and Slices Updated: March 06, 2015
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK