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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Sau Sheong Chang
March 03, 2015
Technology
380
0
Share
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
More Decks by Sau Sheong Chang
See All by Sau Sheong Chang
Genetic Algorithms with Go
sausheong
0
180
Programming Complexity
sausheong
0
1.1k
Rollicking Ruby Robots Rule the World
sausheong
0
290
Money, Sex and Evolution (v3)
sausheong
0
130
Polyglot
sausheong
0
110
Developing Web Applications with Go
sausheong
7
850
Money, Sex and Evolution
sausheong
1
130
A Tale of Two Frameworks
sausheong
0
100
Ruby, Rock and Roll
sausheong
3
310
Other Decks in Technology
See All in Technology
AgentCore Managed Harness を使ってみよう
yakumo
2
270
Practical TypeProf: Lessons from Analyzing Optcarrot
mame
1
1.4k
はじめての MagicPod生成AI機能 機能紹介から活用方法まで
magicpod
0
120
色を視る
yuzneri
0
280
「SaaSの次の時代」に重要性を増すステークホルダーマネジメントの要諦 ~解像度を圧倒的に高めPdMの価値を最大化させる方法~
kakehashi
PRO
3
3.2k
運用システムにおけるデータ活用とPlatform
sansantech
PRO
0
140
Oracle AI Database@AWS:サービス概要のご紹介
oracle4engineer
PRO
4
2.4k
AzureのIaC管理からログ調査まで、随所に役立つSkillsとCustom-Instructions / Boosting IaC and Log Analysis with Skills
aeonpeople
0
340
AIが盛んな時代に 技術記事を書き始めて起きた私の中での小さな変化
peintangos
0
320
AIが書いたコードを信じられない問題 〜レビュー負荷を下げるために変えたこと〜 / The AI Code Trust Gap: Reducing the Review Burden
bitkey
PRO
8
1.4k
GitHub Copilot Dev Days
tomokusaba
0
110
AI バイブコーティングでキーボード不要?!
samakada
0
660
Featured
See All Featured
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
1.9k
Practical Orchestrator
shlominoach
191
11k
How to train your dragon (web standard)
notwaldorf
97
6.6k
We Are The Robots
honzajavorek
0
220
The Curious Case for Waylosing
cassininazir
0
320
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
160
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
140
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.3k
How GitHub (no longer) Works
holman
316
150k
Designing Powerful Visuals for Engaging Learning
tmiket
1
350
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
120
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