Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
3 Things You May Not Know About The Go Template...
Search
Sau Sheong Chang
March 03, 2015
Technology
0
280
3 Things You May Not Know About The Go Template Engine
A short introduction to the Go template engine
Sau Sheong Chang
March 03, 2015
Tweet
Share
More Decks by Sau Sheong Chang
See All by Sau Sheong Chang
Genetic Algorithms with Go
sausheong
0
110
Programming Complexity
sausheong
0
800
Rollicking Ruby Robots Rule the World
sausheong
0
220
Money, Sex and Evolution (v3)
sausheong
0
64
Polyglot
sausheong
0
59
Developing Web Applications with Go
sausheong
7
760
Money, Sex and Evolution
sausheong
1
72
A Tale of Two Frameworks
sausheong
0
69
Ruby, Rock and Roll
sausheong
3
280
Other Decks in Technology
See All in Technology
신뢰할 수 있는 AI 검색 엔진을 만들기 위한 Liner의 여정
huffon
0
530
今、始める、第一歩。 / Your first step
yahonda
2
660
「視座」の上げ方が成人発達理論にわかりやすくまとまってた / think_ perspective_hidden_dimensions
shuzon
2
15k
透過型SMTPプロキシによる送信メールの可観測性向上: Update Edition / Improved observability of outgoing emails with transparent smtp proxy: Update edition
linyows
2
150
「 SharePoint 難しい」ってよく聞くけど、そんなに言うなら8歳の息子に試してもらった
taichinakamura
2
780
VPC間の接続方法を整理してみた #自治体クラウド勉強会
non97
1
1.1k
リンクアンドモチベーション ソフトウェアエンジニア向け紹介資料 / Introduction to Link and Motivation for Software Engineers
lmi
4
300k
re:Invent 2024のおすすめセッション
beli68
0
110
全社横断データ活用推進のコツと その負債とのつき合い方
masatoshi0205
0
160
Forget efficiency – Become more productive without the stress
ufried
0
230
What to do after `laravel new`
mattstauffer
0
130
[JAWS-UG金沢支部×コンテナ支部合同企画]コンテナとは何か
furuton
3
330
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
133
8.9k
It's Worth the Effort
3n
183
27k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
The World Runs on Bad Software
bkeepers
PRO
65
11k
How GitHub (no longer) Works
holman
310
140k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
7
520
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Intergalactic Javascript Robots from Outer Space
tanoku
268
27k
Building Your Own Lightsaber
phodgson
102
6.1k
Transcript
3 Things You May Not Know About The Go
Template Engine Chang Sau Sheong Feb 2015
Obligatory stuff
What is a template engine?
None
None
Where’s Go template engine? Logic-‐less Go template
engine Embedded logic Mustache, Handlebars etc JSP, Haml, Jade etc
How does the Go template engine work?
text/template html/template
Parse template Execute template
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Go Web
Programming</title> </head> <body> {{ . }} </body> </html>
package main import ( "net/http" "html/template" ) func process(w http.ResponseWriter,
r *http.Request) { t, _ := template.ParseFiles("tmpl.html") t.Execute(w, "Hello World!") } func main() { server := http.Server{ Addr: "127.0.0.1:8080", } http.HandleFunc("/process", process) server.ListenAndServe() }
The Go template engine is kinda cool
1. Pipelines
{{ p1 | p2 | p3 }}
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/ html; charset=utf-8"> <title>Go
Web Programming</title> </head> <body> {{ 12.3456 | printf "%.2f" }} </body> </html>
2. FuncTons
func formatDate(t time.Time) string { layout := "2006-01-02" return t.Format(layout)
} func process(w http.ResponseWriter, r *http.Request) { funcMap := template.FuncMap { "fdate": formatDate } t := template.New("tmpl.html").Funcs(funcMap) t, _ = t.ParseFiles("tmpl.html") t.Execute(w, time.Now()) }
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Go Web Programming</title> </head>
<body> <div>The date/time is {{ fdate . }}</div> </body> </html>
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Go Web Programming</title> </head>
<body> <div>The date/time is {{ . | fdate }}</div> </body> </html>
3. Context aware
func process(w http.ResponseWriter, r *http.Request) { t, _ := template.ParseFiles("tmpl.html")
content := `I asked: <i>"What's up?"</i>` t.Execute(w, content) }
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Go Web Programming</title> </head>
<body> <div>{{ . }}</div> <div><a href="/{{ . }}">Path</a></div> <div><a href="/?q={{ . }}">Query</a></div> <div><a onclick="f('{{ . }}')">Onclick</a></div> </body> </html>
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Go Web Programming</title> </head>
<body> <div>I asked: <i>"What's up?"</ i></div> <div><a href="/I%20asked:%20%3ci%3e%22What%27s%20up? %22%3c/i%3e">Path</a></div> <div><a href="/?q=I%20asked%3a%20%3ci%3e%22What%27s %20up%3f%22%3c%2fi%3e">Query</a></div> <div><a onclick="f('I asked: \x3ci\x3e\x22What\x27s up?\x22\x3c\/i\x3e')">Onclick</a></div> </body> </html>
Context Content Original text I asked: <i>"What's up?"</i> {{ .
}} I asked: <i>"What's up?"</i> <a href="/{{ . }}"> I%20asked:%20%3ci%3e%22What%27s%20up?%22%3c/i%3e <a href="/?q={{ . }}"> I%20asked%3a%20%3ci%3e%22What%27s%20up%3f%22%3c %2fi%3e <a onclick="{{ . }}"> I asked: \x3ci\x3e\x22What\x27s up?\x22\x3c\/i\x3e
XSS
XSS Demo
hWp://manning.com/chang
Thank you
[email protected]
@sausheong