Skip to content

Commit

Permalink
template
Browse files Browse the repository at this point in the history
  • Loading branch information
rasheedmhd committed Aug 31, 2024
1 parent ad34e53 commit e95f52d
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions go/tgpl/ch4/issuesreport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const templ = `{{.TotalCount}} issues:
{{range .Items}}----------------------------------------
Number: {{.Number}}
User: {{.User.Login}}
Title: {{.Title | printf "%.64s"}}
Age: {{.CreatedAt | daysAgo}} days
{{end}}`

func daysAgo(t time.Time) int {
return int(time.Since(t).Hours() / 24)
}

report, err := template.New("report").
Funcs(template.FuncMap{"daysAgo": daysAgo}).
Parse(templ)

if err != nil {
log.Fatal(err)
}

var report = template.Must(template.New("issuelist").
Funcs(template.FuncMap{"daysAgo": daysAgo}).
Parse(templ))

func main() {
result, err := github.SearchIssues(os.Args[1:])

if err != nil {
log.Fatal(err)
}

if err := report.Execute(os.Stdout, result); err != nil {
log.Fatal(err)
}
}


// html template
import "html/template"
var issueList = template.Must(template.New("issuelist").Parse(`
<h1>{{.TotalCount}} issues</h1>
<table>
<tr style='text-align: left'>
<th>#</th>
<th>State</th>
<th>User</th>
<th>Title</th>
</tr>
{{range .Items}}
<tr>
<td><a href='{{.HTMLURL}}'>{{.Number}}</td>
<td>{{.State}}</td>
<td><a href='{{.User.HTMLURL}}'>{{.User.Login}}</a></td>
<td><a href='{{.HTMLURL}}'>{{.Title}}</a></td>
</tr>
{{end}}
</table>
`))


// autoescape
func main() {
const templ = `<p>A: {{.A}}</p><p>B: {{.B}}</p>`

t := template.Must(template.New("escape").Parse(templ))
var data struct {
A string // untrusted plain text
B template.HTML // trusted HTML
}

data.A = "<b>Hello!</b>"
data.B = "<b>Hello!</b>"

if err := t.Execute(os.Stdout, data); err != nil {
log.Fatal(err)
}

}

0 comments on commit e95f52d

Please sign in to comment.