templates.adoc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. [#templates]
  2. = Templates in actions
  3. OliveTin uses https://pkg.go.dev/text/template[Go text/template] syntax in action fields such as `shell`, `shellAfterCompleted`, entity directory titles, and `enabledExpression`. Template placeholders are written as `{{ ... }}`.
  4. In OliveTin 3k, use dotted names for template context variables:
  5. * `{{ .Arguments.NAME }}` — argument values (see xref:args/env.adoc[Environment variables])
  6. * `{{ .CurrentEntity.property }}` — entity properties (see xref:entities/intro.adoc[Entities])
  7. * `{{ .Env.VAR_NAME }}` — process environment (see xref:advanced_configuration/config_envs.adoc#using-env-in-template-replacements[Using .Env in template replacements])
  8. * `{{ .OliveTin.Build.Version }}` and related build/runtime fields
  9. IMPORTANT: Unlike argument values, `.CurrentEntity` and `.Env` are **not** sanitized for shell safety when used in `shell` or `shellAfterCompleted`. They are treated as server-controlled data; the config author is responsible for that trust. See xref:action_execution/shellvsexec.adoc#shell-entity-env-trust[Entity and .Env values are not shell-sanitized].
  10. In OliveTin 2k, argument and execution-request placeholders used the shorter form (for example, `{{ message }}` instead of `{{ .Arguments.message }}`).
  11. [#json-encoding]
  12. == JSON encoding with `Json`
  13. The `Json` template function encodes a value as a JSON string. Pipe a template value to it when you need structured data in a command — for example, passing argument or entity state to a script or HTTP client that expects JSON.
  14. [source,yaml]
  15. ----
  16. actions:
  17. - title: curl my knx thing
  18. shell: curl --json '{{ .Arguments | Json }}' https://knx.example.com/v1/group/global_on/write
  19. entity: light
  20. arguments:
  21. - name: value
  22. default: "true"
  23. ----
  24. After template substitution, `{{ .Arguments | Json }}` becomes a JSON object containing all argument names and values for that execution (including execution-request variables such as `ot_username` and `ot_executionTrackingId`).
  25. === Examples
  26. Encode a single argument value:
  27. [source,yaml]
  28. ----
  29. shell: echo {{ .Arguments.value | Json }}
  30. ----
  31. If `value` is `hello`, the substituted command is `echo "hello"`.
  32. Encode an entity field:
  33. [source,yaml]
  34. ----
  35. shell: curl -d {{ .CurrentEntity.foo.bar | Json }}
  36. ----
  37. If `foo.bar` is the string `baz`, the substituted command is `curl -d "baz"`.
  38. Encode a nested entity object:
  39. [source,yaml]
  40. ----
  41. shell: curl --json -d {{ .CurrentEntity.payload | Json }}
  42. ----
  43. If `payload` is `{on: true}`, the substituted command is `curl --json -d {"on":true}`.
  44. === Notes
  45. . `Json` uses Go's `encoding/json` package. Strings, numbers, booleans, objects, and arrays are encoded according to normal JSON rules.
  46. . Argument values in templates are strings (`map[string]string`). A checkbox or boolean argument therefore appears in JSON as a string (for example, `"true"`), not a JSON boolean.
  47. . If the piped value is missing or nil, `Json` produces `null`.
  48. . When embedding JSON in a shell command, quote the substitution if the JSON may contain spaces or shell metacharacters. Prefer single-quoted YAML strings around the template when possible, as shown in the curl example above.
  49. . For HTTP request bodies, pass one JSON-encoded value (or build the JSON structure you need in one template expression). Piping several values with spaces between them does not produce a single valid JSON document.
  50. See link:https://github.com/OliveTin/OliveTin/issues/829[GitHub issue #829] for the original feature request.