Skip to content

Commit

Permalink
Renames EscapeString() to Escape() and adds an example
Browse files Browse the repository at this point in the history
  • Loading branch information
aymerick committed Jun 3, 2015
1 parent d108658 commit 1dfcf65
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,21 +170,21 @@ Output:
</div>
```

When returning HTML from a helper, you should return a `SafeString` if you don't want it to be escaped by default. When using `SafeString` all unknown or unsafe data should be manually escaped with the `EscapeString` method.
When returning HTML from a helper, you should return a `SafeString` if you don't want it to be escaped by default. When using `SafeString` all unknown or unsafe data should be manually escaped with the `Escape` method.

```go
tpl := raymond.MustParse("{{{link text url}}}")
tpl := raymond.MustParse("{{{link url text}}}")

tpl.RegisterHelper("link", func(h *raymond.HelperArg) interface{} {
text := raymond.EscapeString(h.ParamStr(0))
url := raymond.EscapeString(h.ParamStr(1))
url := raymond.Escape(h.ParamStr(0))
text := raymond.Escape(h.ParamStr(1))

return raymond.SafeString("<a href='" + url + "'>" + text + "</a>")
})

ctx := map[string]string{
"text": "This is a <em>cool</em> website",
"url": "http://www.aymerick.com/",
"text": "This is a <em>cool</em> website",
}

result := tpl.MustExec(ctx)
Expand Down
6 changes: 4 additions & 2 deletions escape.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ func escape(w writer, s string) error {
return err
}

// EscapeString escapes special HTML characters.
func EscapeString(s string) string {
// Escape escapes special HTML characters.
//
// It can be used by helpers that return a SafeString and that need to escape some content by themselves.
func Escape(s string) string {
if strings.IndexAny(s, escapedChars) == -1 {
return s
}
Expand Down
23 changes: 23 additions & 0 deletions escape_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package raymond

import "fmt"

func ExampleEscape() {
tpl := MustParse("{{{link url text}}}")

tpl.RegisterHelper("link", func(h *HelperArg) interface{} {
url := Escape(h.ParamStr(0))
text := Escape(h.ParamStr(1))

return SafeString("<a href='" + url + "'>" + text + "</a>")
})

ctx := map[string]string{
"url": "http://www.aymerick.com/",
"text": "This is a <em>cool</em> website",
}

result := tpl.MustExec(ctx)
fmt.Print(result)
// Output: <a href='http://www.aymerick.com/'>This is a &lt;em&gt;cool&lt;/em&gt; website</a>
}
2 changes: 1 addition & 1 deletion eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ func (v *evalVisitor) VisitMustache(node *ast.MustacheStatement) interface{} {
str := Str(expr)
if !isSafe && !node.Unescaped {
// escape html
str = EscapeString(str)
str = Escape(str)
}

return str
Expand Down

0 comments on commit 1dfcf65

Please sign in to comment.