1. Ian Lewis
  2. golang_guestbook

Commits

Ian Lewis  committed cc35c2c

Change newlines to <br> tags

  • Participants
  • Parent commits fec8ee8
  • Branches default

Comments (0)

Files changed (2)

File app/main.go

View file
     }
 }
 
+func newlineFormatter(wr io.Writer, formatter string, data ...interface{}) {
+    for _, item := range data {
+        b, _ := item.([]byte)
+        s := string(b)
+        fmt.Fprintf(wr, strings.Replace(s, "\n", "<br/>", -1))
+    }
+}
+
 func init() {
     http.HandleFunc("/", func(w http.ResponseWriter, request *http.Request) {
         c := appengine.NewContext(request)
         fm := template.FormatterMap{}
         fm["date"] = dateFormatter
         fm["userName"] = userNameFormatter
+        fm["newlinesbr"] = newlineFormatter
         t, _ := template.ParseFile("templates/base.html", fm)
 
         current_user := user.Current(c)

File templates/base.html

View file
   <div>
     User: {AccountEmail|userName}<br/>
     {Date|date}
-    <p style="padding-left:10px">{Body|html}</p>
+    <p style="padding-left:10px">{Body|html|newlinesbr}</p>
   </div>
   {.end}