context.go 3.0 KB

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