mirror of
https://github.com/golang/go
synced 2024-11-22 04:34:39 -07:00
exp/template: url filter.
R=r CC=golang-dev https://golang.org/cl/4837063
This commit is contained in:
parent
ec010fddb5
commit
a09ba8b638
@ -244,6 +244,9 @@ Predefined global functions are named as follows.
|
|||||||
An alias for fmt.Sprintf
|
An alias for fmt.Sprintf
|
||||||
println
|
println
|
||||||
An alias for fmt.Sprintln
|
An alias for fmt.Sprintln
|
||||||
|
url
|
||||||
|
Returns the escaped value of the textual representation of
|
||||||
|
its arguments in a form suitable for embedding in a URL.
|
||||||
|
|
||||||
The boolean functions take any zero value to be false and a non-zero value to
|
The boolean functions take any zero value to be false and a non-zero value to
|
||||||
be true.
|
be true.
|
||||||
|
@ -283,6 +283,9 @@ var execTests = []execTest{
|
|||||||
// JavaScript.
|
// JavaScript.
|
||||||
{"js", `{{js .}}`, `It\'d be nice.`, `It'd be nice.`, true},
|
{"js", `{{js .}}`, `It\'d be nice.`, `It'd be nice.`, true},
|
||||||
|
|
||||||
|
// URL.
|
||||||
|
{"url", `{{"http://www.example.org/"|url}}`, "http%3A%2F%2Fwww.example.org%2F", nil, true},
|
||||||
|
|
||||||
// Booleans
|
// Booleans
|
||||||
{"not", "{{not true}} {{not false}}", "false true", nil, true},
|
{"not", "{{not true}} {{not false}}", "false true", nil, true},
|
||||||
{"and", "{{and false 0}} {{and 1 0}} {{and 0 true}} {{and 1 1}}", "false 0 0 1", nil, true},
|
{"and", "{{and false 0}} {{and 1 0}} {{and 0 true}} {{and 1 1}}", "false 0 0 1", nil, true},
|
||||||
|
@ -7,6 +7,7 @@ package template
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"http"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
@ -31,6 +32,7 @@ var funcs = map[string]reflect.Value{
|
|||||||
"print": reflect.ValueOf(fmt.Sprint),
|
"print": reflect.ValueOf(fmt.Sprint),
|
||||||
"printf": reflect.ValueOf(fmt.Sprintf),
|
"printf": reflect.ValueOf(fmt.Sprintf),
|
||||||
"println": reflect.ValueOf(fmt.Sprintln),
|
"println": reflect.ValueOf(fmt.Sprintln),
|
||||||
|
"url": reflect.ValueOf(URLEscaper),
|
||||||
}
|
}
|
||||||
|
|
||||||
// addFuncs adds to values the functions in funcs, converting them to reflect.Values.
|
// addFuncs adds to values the functions in funcs, converting them to reflect.Values.
|
||||||
@ -318,3 +320,16 @@ func JSEscaper(args ...interface{}) string {
|
|||||||
}
|
}
|
||||||
return JSEscapeString(s)
|
return JSEscapeString(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// URLEscaper returns the escaped value of the textual representation of its
|
||||||
|
// arguments in a form suitable for embedding in a URL.
|
||||||
|
func URLEscaper(args ...interface{}) string {
|
||||||
|
s, ok := "", false
|
||||||
|
if len(args) == 1 {
|
||||||
|
s, ok = args[0].(string)
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
s = fmt.Sprint(args...)
|
||||||
|
}
|
||||||
|
return http.URLEscape(s)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user