Skip to content

Commit

Permalink
add jinja template example
Browse files Browse the repository at this point in the history
  • Loading branch information
goFrendiAsgard committed Jan 21, 2024
1 parent 32e9e22 commit 5ef03d0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 22 deletions.
10 changes: 9 additions & 1 deletion docs/concepts/runner-group-and-task.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,15 @@ Using Task Group, you can make your Tasks more organized.
</p>
</div>

Tasks are the most basic unit in Zrb. There are many Task Classes you can use to create a Task.
Tasks are the most basic unit in Zrb. Typically, a Zrb Task has some builtin settings.

- Retry mechanism.
- Upstreams/Dependencies.
- Environment and Environment File.
- Task Input/Parameter.
- Readiness Checker.

There are many Task Classes you can use to create a Task.

- [Task](../technical-documentation/tasks/task.md): General purpose class, usually created using [@python_task](../technical-documentation/tasks/python-task.md) decorator.
- [CmdTask](../technical-documentation/tasks/cmd-task.md): Run a CLI command/shell script.
Expand Down
74 changes: 56 additions & 18 deletions docs/concepts/template-rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,48 @@

# Template Rendering

Most Task, Env, and Input properties accept Jinja syntax as a value. Let's see some of them.

Let's see some available objects in Zrb's Jinja template:

- `datetime`: Python datetime module.
- `os`: Python os module
- `platform`: Python platform module.
- `time`: Python time module.
- `util`: Zrb utilities.
Many properties in Zrb accept a Jinja Template. You can ensure whether a property accepts a Jinja Template by checking on its annotation. Any property with `JinjaTemplate` annotation accepts a Jinja Template value.

You can learn more about Jinja [here](https://jinja.palletsprojects.com/en/2.10.x/templates/). The basic idea is as follows:

- Showing a variable
```
{{ variable_name }}
```
- Showing a property
```
{{ some_object.some_property }}
```
- Showing a return value of a function
```
{{ some_object.some_function() }}
or
{{ some_function() }}
```
- Branching
```
{% if True %}
Ok
{% else %}
Not Ok
{% endif %}
```
- Loop
```
{% for item in navigation %}
<a href="{{ item.href }}">{{ item.caption }}</a>
{% endfor %}
```
# Available Objects
Zrb automatically adds some objects and common Python packages you can access while using the Jinja Template.
- `datetime` (Always accessible): Python datetime module.
- `os` (Always accessible): Python os module
- `platform` (Always accessible): Python platform module.
- `time` (Always accessible): Python time module.
- `util` (Always accessible): Zrb utilities.
- `util.coalesce(value, *alternatives)`: Coalesce a value with the alternatives sequentially. An empty string is considered as a value.
- `util.coalesce_str(value, *alternatives)`: Coalesce a value with the alternatives sequantially. An empty string is not considered as a value.
- `util.to_camel-case(text)`: Returns a `camelCased` text.
Expand All @@ -22,19 +55,21 @@ Let's see some available objects in Zrb's Jinja template:
- Returns `True` if text is either `true`, `1`, `yes`, `y`, `active`, or `on`.
- Returns `False` if text is either `fales`, `0`, `no`, `n`, `inactive`, or `off`.
- Raises Exception otherwise.
- `input`: Input value dictionary. The dictionary keys are __snake_cased__ Input names, while the dictionary values are the rendered Input values.
- `env`: Env value dictionary. The dictionary keys are Env names, while the dictionary values are the rendered Env values. Under the hood, Zrb renders an EnvFile into multiple Envs. Thus, all variables in your environment file will be accessible from the `env` dictionary.
- `task`: Current Task object.
- `task.get_env_map()`: Returning `env` dictionary.
- `task.get_input_map()`: Returning `input` dictionary.
- `task.set_xcom(key, value)`: Returning an empty string after setting an XCom key.
- `task.get_xcom(key)`: Getting an XCom value.
- `task.get_execution_id()`: Getting Execution ID
- `input` (Accessible while rendering `Env`, `EnvFile`, and other Task Properties): Input value dictionary. The dictionary keys are __snake_cased__ Input names, while the dictionary values are the rendered Input values.
- `env` (Accessible while rendering Task Properties): Env value dictionary. The dictionary keys are Env names, while the dictionary values are the rendered Env values. Under the hood, Zrb renders an EnvFile into multiple Envs. Thus, all variables in your environment file will be accessible from the `env` dictionary.
- `task` (Available on runtime): Current Task object.
- `task.get_input_map()` (Available after input is rendered): Returning `input` dictionary.
- `task.get_env_map()` (Available after input and env is rendered): Returning `env` dictionary.
- `task.set_xcom(key, value)` (Available on runtime): Returning an empty string after setting an XCom key.
- `task.get_xcom(key)` (Available on runtime): Getting an XCom value.
- `task.get_execution_id()` (Available on runtime): Getting Execution ID
In the rest of this section you will see how to use Jinja Template as Task property.
# Input
Input has an attribute named `should_render` that defaults to `True`. This attribute makes Zrb render Input's value as a Jinja template.
Input has an attribute named `should_render` that defaults to `True`. This attribute makes Zrb render Input's value as a Jinja Template.
The following objects are accessible from Input's value:
Expand Down Expand Up @@ -430,5 +465,8 @@ Zrb renders Checker attributes as Jinja Template. The detailed renderable attrib
- `ignored_path` (`Union[Iterable[JinjaTemplate], JinjaTemplate]`)
- `should_execute` (`Union[bool, JinjaTemplate, Callable[..., bool]]`)

# Next

Next, you can learn more about [specialized tasks](specialized-tasks/README.md)

🔖 [Table of Contents](../README.md) / [Concepts](README.md)
8 changes: 5 additions & 3 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ Typically, a Zrb Task has multiple settings:
- Task Input/Parameter
- Readiness Checker

To learn more about Task, please visit [the concept section](concepts/README.md).

## Task Definition

<div align="center">
Expand Down Expand Up @@ -381,7 +383,7 @@ def task_name(*args, **kwargs):

The following properties are usually available:

- __name__: The name of the task. When you invoke the task using the CLI, you need to use this name. By convention, the name should-be written in `kebab-case` (i.e., separated by `-`).
- __name__: The name of the Task. When you invoke the task using the CLI, you need to use this name. You should wrote Task name in `kebab-case` (i.e., separated by `-`).
- __description__: The description of the task.
- __group__: The task group to which the task belongs.
- __inputs__: Task inputs and their default values.
Expand Down Expand Up @@ -829,12 +831,12 @@ export ZRB_ENV=DEV
zrb serve
```

Once you do so, you will see that Zrb now shows `My-page-dev` instead of `My-page` as the page title.
Once you do so, you will see that Zrb now shows `My-page-dev` instead of `My-page` as the page title. You can learn more about environment cascading at the [environment section](concepts/environments.md).


# Next

Now you are ready. You can proceed with [concept section](concepts/README.md) to learn more about the details.
Now you are ready. You can proceed with the [concept section](concepts/README.md) to learn more about the details.

Have fun, Happy Coding, and Automate More!!!

Expand Down

0 comments on commit 5ef03d0

Please sign in to comment.