github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/docs/assets/how-to-guides/write-simple-dapp/poll-3.gno (about) 1 func Render(path string) string { 2 var b bytes.Buffer 3 4 b.WriteString("# Polls!\n\n") 5 6 if polls.Size() == 0 { 7 b.WriteString("### No active polls currently!") 8 return b.String() 9 } 10 polls.Iterate("", "", func(key string, value interface{}) bool { 11 12 // cast raw data from tree into Poll struct 13 p := value.(*poll.Poll) 14 ddl := p.Deadline() 15 16 yay, nay := p.VoteCount() 17 yayPercent := 0 18 nayPercent := 0 19 20 if yay+nay != 0 { 21 yayPercent = yay * 100 / (yay + nay) 22 nayPercent = nay * 100 / (yay + nay) 23 } 24 25 b.WriteString( 26 ufmt.Sprintf( 27 "## Poll #%s: %s\n", 28 key, // poll ID 29 p.Title(), 30 ), 31 ) 32 33 dropdown := "<details>\n<summary>Poll details</summary><br>" 34 35 b.WriteString(dropdown + "Description: " + p.Description()) 36 37 b.WriteString( 38 ufmt.Sprintf("<br>Voting until block: %d<br>Current vote count: %d", 39 p.Deadline(), 40 p.Voters().Size()), 41 ) 42 43 b.WriteString( 44 ufmt.Sprintf("<br>YAY votes: %d (%d%%)", yay, yayPercent), 45 ) 46 b.WriteString( 47 ufmt.Sprintf("<br>NAY votes: %d (%d%%)</details>", nay, nayPercent), 48 ) 49 50 dropdown = "<br><details>\n<summary>Vote details</summary>" 51 b.WriteString(dropdown) 52 53 p.Voters().Iterate("", "", func(key string, value interface{}) bool { 54 55 voter := key 56 vote := value.(bool) 57 58 if vote == true { 59 b.WriteString( 60 ufmt.Sprintf("<br>%s voted YAY!", voter), 61 ) 62 } else { 63 b.WriteString( 64 ufmt.Sprintf("<br>%s voted NAY!", voter), 65 ) 66 } 67 return false 68 }) 69 70 b.WriteString("</details>\n\n") 71 return false 72 }) 73 return b.String() 74 }