Add "with" expression for templating #53
Allows optionally rendering content if an argument is given. The expression is designed to be used with `optional` parameters. Goal is to allow using `RunPowerShell` function on every function that consists of PowerShell code. Before this commit, they were all required to provide revertCode, or none of them could be able to have it. It would not work because some scripts can be reverted, meanwhile some are one-way scripts that cannot be reverted (such as cleaning scripts). In this case a way to optionally render revertCode was required. `with` expression give each callee script ability to turn off `revertCode` if not needed, therefore enables using `RunPowerShell` everywhere. This commit also improves error message for script code for better debugging and refactors parser tests for more code reuse. It also adds more tests to parameter substitution, and renames some tests of both expressions for consistency.
This commit is contained in:
@@ -108,6 +108,7 @@
|
||||
#### Expressions
|
||||
|
||||
- Expressions are defined inside mustaches (double brackets, `{{` and `}}`)
|
||||
- Expression syntax is inspired by [Go Templates](https://pkg.go.dev/text/template)
|
||||
|
||||
##### Parameter substitution
|
||||
|
||||
@@ -148,6 +149,25 @@ A function can call other functions such as:
|
||||
code: Hello {{ $argument }} !
|
||||
```
|
||||
|
||||
##### with
|
||||
|
||||
- Skips the block if the variable is absent or empty.
|
||||
- Binds its context (`.`) value of provided argument for the parameter only if its value is provided.
|
||||
- A block is defined as `{{ with $parameterName }} Parameter value is {{ . }} here {{ end }}`
|
||||
- The parameters used for `with` condition should be declared as optional, otherwise `with` block becomes redundant.
|
||||
- Example:
|
||||
|
||||
```yaml
|
||||
function: FunctionThatOutputsConditionally
|
||||
parameters:
|
||||
- name: 'argument'
|
||||
optional: true
|
||||
code: |-
|
||||
{{ with $argument }}
|
||||
$argument's value is: {{ . }}
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
#### `Function` syntax
|
||||
|
||||
- `name`: *`string`* (**required**)
|
||||
@@ -188,6 +208,7 @@ A function can call other functions such as:
|
||||
- Otherwise it throws.
|
||||
- 💡 Set it to `true` if a parameter is used conditionally;
|
||||
- Or else set it to `false` for verbosity or do not define it as default value is `false` anyway.
|
||||
- 💡 Can be used in conjunction with [`with` expression](#with).
|
||||
|
||||
### `ScriptingDefinition`
|
||||
|
||||
|
||||
Reference in New Issue
Block a user