templates.adoc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. In OliveTin 2k, argument and execution-request placeholders used the shorter form (for example, `{{ message }}` instead of `{{ .Arguments.message }}`).
  10. [#json-encoding]
  11. == JSON encoding with `Json`
  12. 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.
  13. [source,yaml]
  14. ----
  15. actions:
  16. - title: curl my knx thing
  17. shell: curl --json '{{ .Arguments | Json }}' https://knx.example.com/v1/group/global_on/write
  18. entity: light
  19. arguments:
  20. - name: value
  21. default: "true"
  22. ----
  23. 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`).
  24. === Examples
  25. Encode a single argument value:
  26. [source,yaml]
  27. ----
  28. shell: echo {{ .Arguments.value | Json }}
  29. ----
  30. If `value` is `hello`, the substituted command is `echo "hello"`.
  31. Encode an entity field:
  32. [source,yaml]
  33. ----
  34. shell: curl -d {{ .CurrentEntity.foo.bar | Json }}
  35. ----
  36. If `foo.bar` is the string `baz`, the substituted command is `curl -d "baz"`.
  37. Encode a nested entity object:
  38. [source,yaml]
  39. ----
  40. shell: curl --json -d {{ .CurrentEntity.payload | Json }}
  41. ----
  42. If `payload` is `{on: true}`, the substituted command is `curl --json -d {"on":true}`.
  43. === Notes
  44. . `Json` uses Go's `encoding/json` package. Strings, numbers, booleans, objects, and arrays are encoded according to normal JSON rules.
  45. . 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.
  46. . If the piped value is missing or nil, `Json` produces `null`.
  47. . 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.
  48. . 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.
  49. See link:https://github.com/OliveTin/OliveTin/issues/829[GitHub issue #829] for the original feature request.