context.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package pongo2
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. var reIdentifiers = regexp.MustCompile("^[a-zA-Z0-9_]+$")
  7. var autoescape = true
  8. func SetAutoescape(newValue bool) {
  9. autoescape = newValue
  10. }
  11. // A Context type provides constants, variables, instances or functions to a template.
  12. //
  13. // pongo2 automatically provides meta-information or functions through the "pongo2"-key.
  14. // Currently, context["pongo2"] contains the following keys:
  15. // 1. version: returns the version string
  16. //
  17. // Template examples for accessing items from your context:
  18. // {{ myconstant }}
  19. // {{ myfunc("test", 42) }}
  20. // {{ user.name }}
  21. // {{ pongo2.version }}
  22. type Context map[string]interface{}
  23. func (c Context) checkForValidIdentifiers() *Error {
  24. for k, v := range c {
  25. if !reIdentifiers.MatchString(k) {
  26. return &Error{
  27. Sender: "checkForValidIdentifiers",
  28. OrigError: fmt.Errorf("context-key '%s' (value: '%+v') is not a valid identifier", k, v),
  29. }
  30. }
  31. }
  32. return nil
  33. }
  34. // Update updates this context with the key/value-pairs from another context.
  35. func (c Context) Update(other Context) Context {
  36. for k, v := range other {
  37. c[k] = v
  38. }
  39. return c
  40. }
  41. // ExecutionContext contains all data important for the current rendering state.
  42. //
  43. // If you're writing a custom tag, your tag's Execute()-function will
  44. // have access to the ExecutionContext. This struct stores anything
  45. // about the current rendering process's Context including
  46. // the Context provided by the user (field Public).
  47. // You can safely use the Private context to provide data to the user's
  48. // template (like a 'forloop'-information). The Shared-context is used
  49. // to share data between tags. All ExecutionContexts share this context.
  50. //
  51. // Please be careful when accessing the Public data.
  52. // PLEASE DO NOT MODIFY THE PUBLIC CONTEXT (read-only).
  53. //
  54. // To create your own execution context within tags, use the
  55. // NewChildExecutionContext(parent) function.
  56. type ExecutionContext struct {
  57. template *Template
  58. Autoescape bool
  59. Public Context
  60. Private Context
  61. Shared Context
  62. }
  63. var pongo2MetaContext = Context{
  64. "version": Version,
  65. }
  66. func newExecutionContext(tpl *Template, ctx Context) *ExecutionContext {
  67. privateCtx := make(Context)
  68. // Make the pongo2-related funcs/vars available to the context
  69. privateCtx["pongo2"] = pongo2MetaContext
  70. return &ExecutionContext{
  71. template: tpl,
  72. Public: ctx,
  73. Private: privateCtx,
  74. Autoescape: autoescape,
  75. }
  76. }
  77. func NewChildExecutionContext(parent *ExecutionContext) *ExecutionContext {
  78. newctx := &ExecutionContext{
  79. template: parent.template,
  80. Public: parent.Public,
  81. Private: make(Context),
  82. Autoescape: parent.Autoescape,
  83. }
  84. newctx.Shared = parent.Shared
  85. // Copy all existing private items
  86. newctx.Private.Update(parent.Private)
  87. return newctx
  88. }
  89. func (ctx *ExecutionContext) Error(msg string, token *Token) *Error {
  90. return ctx.OrigError(fmt.Errorf(msg), token)
  91. }
  92. func (ctx *ExecutionContext) OrigError(err error, token *Token) *Error {
  93. filename := ctx.template.name
  94. var line, col int
  95. if token != nil {
  96. // No tokens available
  97. // TODO: Add location (from where?)
  98. filename = token.Filename
  99. line = token.Line
  100. col = token.Col
  101. }
  102. return &Error{
  103. Template: ctx.template,
  104. Filename: filename,
  105. Line: line,
  106. Column: col,
  107. Token: token,
  108. Sender: "execution",
  109. OrigError: err,
  110. }
  111. }
  112. func (ctx *ExecutionContext) Logf(format string, args ...interface{}) {
  113. ctx.template.set.logf(format, args...)
  114. }