Golang html/template.JSStr type examples

package html/template

JSStr encapsulates a sequence of characters meant to be embedded between quotes in a JavaScript expression. The string must match a series of StringCharacters:

StringCharacter :: SourceCharacter but not \ or LineTerminator | EscapeSequence

Note that LineContinuations are not allowed. JSStr("foo\nbar") is fine, but JSStr("foo\\nbar") is not.

Golang html/template.JSStr type usage examples

Example 1:

 data := []interface{}{
 `<b> "foo%" O'Reilly &bar;`,
 CSS(`a[href =~ "//example.com"]#foo`),
 HTML(`Hello, <b>World</b> &amp;tc!`),
 HTMLAttr(` dir="ltr"`),
 JS(`c && alert("Hello, World!");`),
 JSStr(`Hello, World & O'Reilly\x21`),  // <--- here
 URL(`greeting=H%69&addressee=(World)`),
  }

Example 2:

 ...
 p.Title = title
 slug := filepath.Base(filename)
 length := len(slug)
 p.Slug = slug[:length-3]
 body := MD(buffer.Bytes())
 p.Body = template.HTML(string(body))
 p.Date = get_time(date)
 p.S_Date = date
 p.Tags = tags
 p.Changed = false
 p.Url = fmt.Sprintf("%sposts/%s.html", conf.URL, p.Slug)
 p.Durl = template.JSStr(p.Url)  // <--- here
 ...

References :

https://github.com/kushaldas/shonku/blob/master/scrdkd.go

http://golang.org/pkg/html/template/#JSStr

Advertisement