Jinja2Template
Transforms the incoming data using a defined Jinja2 template
Added in version 1.0.0.
Friendly name:
Jinja Template Data RenderStep class:
base_steps_syncpack.steps.Jinja2Template.Jinja2TemplateStep version:
1.0.0Required parameters:
template
Parameters
Name |
Type |
Required |
Default |
Description |
|---|---|---|---|---|
|
jinja2 template |
yes |
– |
The Jinja2 template to use for rendering desired data |
Every step also accepts the platform parameters common to all steps – see Platform parameters.
Dependencies
Inherits from:
Jinja2Template->BaseStep->SerializableSyncPack modules:
base_steps_syncpack.util.steps_env_utilsExternal packages:
ipaascommon,ipaascore,jinja2
Behaviour
Renders template against the previous step’s data, then tries
json.loads on the result:
parses cleanly — the parsed object is saved;
raises
JSONDecodeError— the raw rendered string is saved instead;renders empty — nothing is saved.
An invalid template raises InvalidJinjaTemplate.
Outputs
Whatever the template produces: a dict or list when the render is valid JSON, otherwise a string. Because the fallback is silent, a template with a stray comma yields a string rather than an error, and the failure surfaces one step later.
Example
Reshaping SL1 rows into a ServiceNow payload:
[
{% for row in previous_step_data %}
{"ci": "{{ row.name }}", "id": {{ row.device_id }}}{{ "," if not loop.last }}
{% endfor %}
]
Template environment
util.steps_env_utils.set_previous_data_step_env builds the names a template can
use:
Name |
Contents |
|---|---|
|
That step’s data. Whitespace becomes an underscore, so a step called
|
|
The same data, by positional index. |
|
All previous step data joined, when the types are compatible. |
|
Number of keys in that joined data. |
Warning
Building the environment adds a ``length`` key to every upstream dict, in place:
v.data["length"] = len(v.data)
So {{ MyStep | tojson }} emits a length field that was never in the
source data, and it carries on to the next step. Drop it explicitly if the
receiving system rejects unexpected fields.
Notes
Emitting JSON from a text template means quoting and comma placement are your problem —
loop.lastas above, or build the structure in a step instead.This is the base class of TriggerApplication, which is why that step also takes a
template.
API
- class base_steps_syncpack.steps.Jinja2Template.Jinja2Template
Bases:
BaseStep- get_template()
Returns the template to be used when rendering data
- Returns:
Template
- render_data(input_kwargs, template)
Renders the desired data using the previous step data and provided template
- Parameters:
input_kwargs – dict()
template – Template
Returns: