default.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2016 José Santos <henrique_1609@me.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package jet
  15. import (
  16. "encoding/json"
  17. "errors"
  18. "html"
  19. "io"
  20. "io/ioutil"
  21. "net/url"
  22. "reflect"
  23. "strings"
  24. "text/template"
  25. )
  26. var defaultVariables map[string]reflect.Value
  27. func init() {
  28. defaultVariables = map[string]reflect.Value{
  29. "lower": reflect.ValueOf(strings.ToLower),
  30. "upper": reflect.ValueOf(strings.ToUpper),
  31. "hasPrefix": reflect.ValueOf(strings.HasPrefix),
  32. "hasSuffix": reflect.ValueOf(strings.HasSuffix),
  33. "repeat": reflect.ValueOf(strings.Repeat),
  34. "replace": reflect.ValueOf(strings.Replace),
  35. "split": reflect.ValueOf(strings.Split),
  36. "trimSpace": reflect.ValueOf(strings.TrimSpace),
  37. "html": reflect.ValueOf(html.EscapeString),
  38. "url": reflect.ValueOf(url.QueryEscape),
  39. "safeHtml": reflect.ValueOf(SafeWriter(template.HTMLEscape)),
  40. "safeJs": reflect.ValueOf(SafeWriter(template.JSEscape)),
  41. "raw": reflect.ValueOf(SafeWriter(unsafePrinter)),
  42. "unsafe": reflect.ValueOf(SafeWriter(unsafePrinter)),
  43. "writeJson": reflect.ValueOf(jsonRenderer),
  44. "json": reflect.ValueOf(json.Marshal),
  45. "map": reflect.ValueOf(newMap),
  46. "slice": reflect.ValueOf(newSlice),
  47. "array": reflect.ValueOf(newSlice),
  48. "isset": reflect.ValueOf(Func(func(a Arguments) reflect.Value {
  49. a.RequireNumOfArguments("isset", 1, -1)
  50. for i := 0; i < len(a.argExpr); i++ {
  51. if !a.runtime.isSet(a.argExpr[i]) {
  52. return valueBoolFALSE
  53. }
  54. }
  55. return valueBoolTRUE
  56. })),
  57. "len": reflect.ValueOf(Func(func(a Arguments) reflect.Value {
  58. a.RequireNumOfArguments("len", 1, 1)
  59. expression := a.Get(0)
  60. if expression.Kind() == reflect.Ptr || expression.Kind() == reflect.Interface {
  61. expression = expression.Elem()
  62. }
  63. switch expression.Kind() {
  64. case reflect.Array, reflect.Chan, reflect.Slice, reflect.Map, reflect.String:
  65. return reflect.ValueOf(expression.Len())
  66. case reflect.Struct:
  67. return reflect.ValueOf(expression.NumField())
  68. }
  69. a.Panicf("len(): invalid value type %s", expression.Type())
  70. return reflect.Value{}
  71. })),
  72. "includeIfExists": reflect.ValueOf(Func(func(a Arguments) reflect.Value {
  73. a.RequireNumOfArguments("includeIfExists", 1, 2)
  74. t, err := a.runtime.set.GetTemplate(a.Get(0).String())
  75. // If template exists but returns an error then panic instead of failing silently
  76. if t != nil && err != nil {
  77. panic(err)
  78. }
  79. if err != nil {
  80. return hiddenFalse
  81. }
  82. a.runtime.newScope()
  83. defer a.runtime.releaseScope()
  84. a.runtime.blocks = t.processedBlocks
  85. root := t.Root
  86. if t.extends != nil {
  87. root = t.extends.Root
  88. }
  89. if a.NumOfArguments() > 1 {
  90. c := a.runtime.context
  91. defer func() { a.runtime.context = c }()
  92. a.runtime.context = a.Get(1)
  93. }
  94. a.runtime.executeList(root)
  95. return hiddenTrue
  96. })),
  97. "exec": reflect.ValueOf(Func(func(a Arguments) (result reflect.Value) {
  98. a.RequireNumOfArguments("exec", 1, 2)
  99. t, err := a.runtime.set.GetTemplate(a.Get(0).String())
  100. if err != nil {
  101. panic(err)
  102. }
  103. a.runtime.newScope()
  104. defer a.runtime.releaseScope()
  105. w := a.runtime.Writer
  106. defer func() { a.runtime.Writer = w }()
  107. a.runtime.Writer = ioutil.Discard
  108. a.runtime.blocks = t.processedBlocks
  109. root := t.Root
  110. if t.extends != nil {
  111. root = t.extends.Root
  112. }
  113. if a.NumOfArguments() > 1 {
  114. c := a.runtime.context
  115. defer func() { a.runtime.context = c }()
  116. a.runtime.context = a.Get(1)
  117. }
  118. result = a.runtime.executeList(root)
  119. return result
  120. })),
  121. "ints": reflect.ValueOf(Func(func(a Arguments) (result reflect.Value) {
  122. var from, to int
  123. err := a.ParseInto(&from, &to)
  124. if err != nil {
  125. panic(err)
  126. }
  127. // check to > from
  128. if to <= from {
  129. panic(errors.New("invalid range for ints ranger: 'from' must be smaller than 'to'"))
  130. }
  131. return reflect.ValueOf(&intsRanger{from: from, to: to})
  132. })),
  133. }
  134. }
  135. type hiddenBool bool
  136. func (m hiddenBool) Render(r *Runtime) { /* render nothing -> hidden */ }
  137. var hiddenTrue = reflect.ValueOf(hiddenBool(true))
  138. var hiddenFalse = reflect.ValueOf(hiddenBool(false))
  139. func jsonRenderer(v interface{}) RendererFunc {
  140. return func(r *Runtime) {
  141. err := json.NewEncoder(r.Writer).Encode(v)
  142. if err != nil {
  143. panic(err)
  144. }
  145. }
  146. }
  147. func unsafePrinter(w io.Writer, b []byte) {
  148. w.Write(b)
  149. }
  150. // SafeWriter is a function that writes bytes directly to the render output, without going through Jet's auto-escaping phase.
  151. // Use/implement this if content should be escaped differently or not at all (see raw/unsafe builtins).
  152. type SafeWriter func(io.Writer, []byte)
  153. var stringType = reflect.TypeOf("")
  154. var newMap = Func(func(a Arguments) reflect.Value {
  155. if a.NumOfArguments()%2 > 0 {
  156. panic("map(): incomplete key-value pair (even number of arguments required)")
  157. }
  158. m := reflect.ValueOf(make(map[string]interface{}, a.NumOfArguments()/2))
  159. for i := 0; i < a.NumOfArguments(); i += 2 {
  160. key := a.Get(i)
  161. if !key.Type().ConvertibleTo(stringType) {
  162. a.Panicf("map(): can't use %+v as string key: %s is not convertible to string", key, key.Type())
  163. }
  164. key = key.Convert(stringType)
  165. m.SetMapIndex(a.Get(i), a.Get(i+1))
  166. }
  167. return m
  168. })
  169. var newSlice = Func(func(a Arguments) reflect.Value {
  170. arr := make([]interface{}, a.NumOfArguments())
  171. for i := 0; i < a.NumOfArguments(); i++ {
  172. arr[i] = a.Get(i).Interface()
  173. }
  174. return reflect.ValueOf(arr)
  175. })