# Jinja: (Custom) variables, wildcards and functions

**Jinja2**, one of the world's most popular data rendering engines, is used as a templating language to render individual information. Many core functions are available. Some are not available for security reasons. A list of the core external functions can be found under:

* [Variables](https://jinja.palletsprojects.com/en/2.10.x/templates/#variables)
* [Filters](https://jinja.palletsprojects.com/en/2.10.x/templates/#filters)
* [Tests](https://jinja.palletsprojects.com/en/2.10.x/templates/#tests)
* [Comments](https://jinja.palletsprojects.com/en/2.10.x/templates/#comments)
* [Escaping](https://jinja.palletsprojects.com/en/2.10.x/templates/#escaping)
* [Line Statements](https://jinja.palletsprojects.com/en/2.10.x/templates/#line-statements)
* [Template Objects](https://jinja.palletsprojects.com/en/2.10.x/templates/#template-objects)
* [Working with Manual Escaping](https://jinja.palletsprojects.com/en/2.10.x/templates/#working-with-manual-escaping)
* [Working with Automatic Escaping](https://jinja.palletsprojects.com/en/2.10.x/templates/#working-with-automatic-escaping)
* [All about loops and accessing objects with loops](https://jinja.palletsprojects.com/en/2.10.x/templates/#list-of-control-structures)
* [For](https://jinja.palletsprojects.com/en/2.10.x/templates/#for)
* [If](https://jinja.palletsprojects.com/en/2.10.x/templates/#if)
* [Call](https://jinja.palletsprojects.com/en/2.10.x/templates/#call)
* [Filters](https://jinja.palletsprojects.com/en/2.10.x/templates/#id11)
* [Variable assignments](https://jinja.palletsprojects.com/en/2.10.x/templates/#assignments)
* [Math](https://jinja.palletsprojects.com/en/2.10.x/templates/#math)
* [Comparisons](https://jinja.palletsprojects.com/en/2.10.x/templates/#comparisons)
* [Logic](https://jinja.palletsprojects.com/en/2.10.x/templates/#logic)
* [Other Operators](https://jinja.palletsprojects.com/en/2.10.x/templates/#other-operators)
* [List of built-in Filters](https://jinja.palletsprojects.com/en/2.10.x/templates/#list-of-builtin-filters)
* [Expression Statement](https://jinja.palletsprojects.com/en/2.10.x/templates/#expression-statement)
* [Loop Controls](https://jinja.palletsprojects.com/en/2.10.x/templates/#loop-controls)

## Custom Locoia variables

Custom Locoia variables can be found in the Functions Reference Panel in the Flow Builder and via autocomplete:

<figure><img src="https://291121471-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-McrRFZHYH27bqKzOVDd%2Fuploads%2FhF5FZDnA61vdRXbF8GbN%2Fimage.png?alt=media&#x26;token=d58582f4-36b2-4158-a25e-c9bcc4a3f169" alt=""><figcaption></figcaption></figure>

### Additional details

For some functions there're additional details to consider.

#### regex\_replace

In order to capture groups with this function  `\g<name>` needs to be used with named groups and `\g<number>` with plain groups.

## Noteworthy core Jinja functions

### selectattr

* [Official documentation](https://jinja.palletsprojects.com/en/3.0.x/templates/#jinja-filters.selectattr)
* Usage:&#x20;

  ```django
  {% for record in list_reference | selectattr(field_reference, 'match', 'comparison_value') %}
  ```
* Use case: This lets you filter any (nested) list directly in Jinja by specific attributes

### namespace()

* [Official documentation](https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-globals.namespace)
* Usage: \
  The `namespace` object (container) is an easy way to create an object with attributes. It helps avoid conflicts with variable names and provides a clean and concise way to store and retrieve values within the context of a template.

  This is particularly useful when carrying a value from within a loop body to outside the loop, which is not possible with standard variables.
* Example:\
  This code snippet generates a list of records, separating them with commas. It utilizes a dedicated `comma_set` object to track whether a comma has been set for any record so that it can be placed correctly for a valid JSON. As this variable value needs to be carried across loop iterations, a namespace needs to be used.

```django
{% set comma_set = namespace(first_comma_set=False) %}
[
  {% for record in records %}
    {% if record ... %}
      {% if comma_set.first_comma_set %},
      {% else %}
        {% set comma_set.first_comma_set = True %}
      {% endif %}
      {{ record }}
    {% endif %}
  {% endfor %}    
]
```

### get()

* [Official documentation](https://docs.python.org/2/library/stdtypes.html#dict.get)
* Usage:&#x20;

The `get()` function in Jinja (a standard Python dictionary method) allows you to efficiently map a dictionary using key-value pairs. It is particularly useful when dealing with scenarios where you need to map values based on specific keys.

* Example\
  Imagine you have to map a dictionary with the following key-value pairs:

```python
{% set type_mapping = 
  {
    'Wohnung': 'APARTMENT', 
    'Stellplatz außen': 'PARKING', 
    'Garagen': 'PARKING'
  } 
%}
```

Traditionally, you might use an if-else statement to perform the mapping:

```python
{% if unit_type in type_mapping %}
  "{{ type_mapping[unit_type] }}"
{% else %}
  "DEFAULT VALUE"
{% endif %}
```

However, to minimize code and enhance readability, you can achieve the same result using the `get()` function:

```python
{{ type_mapping.get(unit_type, "DEFAULT VALUE") }}
```

The `get()` function retrieves the value associated with the specified `unit_type` from the dictionary. If the `unit_type` is not found, it defaults to the specified `"DEFAULT VALUE"`. This approach simplifies the code and improves maintainability.
