Darwinium Templating can be used to create any text-based format such as HTML, CSV, XML, JSON etc. A template can be a file or string with templating enabled.
Darwinium Templating implements a sub-set of Jinja2 syntax and supports most features of Jinja2, which will be familiar to users of Django and Python.
Expressions
Darwinium Templating allows basic expressions everywhere. To output the result of an expression wrap it in double braces {{ .. }}.
For example, the following template:
{{ "Hello From Darwinium!" }}would produce the following output:
Hello From Darwinium!For example:
{{ 'hello' 'from' 'earth' }} would produce the output "hellofromearth" however {{ 'hello ' 'from ' 'space !' }} would produce the output "hello from space !"
Literals
The simplest form of expressions are literals. Literals are representations for Python objects such as string and numbers. The following literals are supported:
- Strings - everything between either a pair of double or single quotes is a string. e.g "Darwinium Injection" and 'Darwinium Injection' are both examples of valid strings.
- Integers - are whole numbers without a decimal part. e.g 42 is an integer.
- Floating point (real) numbers - are fractional numbers with a decimal part. e.g 42.2 is a floating point number
- List - everything between two brackets is a list. e.g ('list', 'of', 'strings') is a list containing three strings.
- Map - A map is a structure that combines keys and values, also known as an "associative array" or "name/value array". Map keys must be unique and only have one value. Maps are represented using a pair of single braces and very similar to JSON. e.g. { 'map' : 'of', 'key' : 'and' , 'value' : 'pairs' }
- Boolean - booleans only have two states and can be true or false
Variables
Darwinium Attributes are available as variables inside Templates.
Assuming the Attribute "identity['ACCOUNT'].name.first was set to the value 'John' and the Attribute identity['ACCOUNT'].name.last was set to the value 'Smith' then the following template:
{{ identity['ACCOUNT'].name.first ~ ' ' ~ identity['ACCOUNT'].name.last }}would render the following output:
John SmithExample: Arrays
A Darwinium array attribute (like array of string signals) can be parsed in a template by looping the entries.
{
"signals": [
{% for signal in profiling.device.signals %}
"{{ signal }}"
{%- if not loop.last %},{%- endif %}
{% endfor %}
]
}
Example: Maps
A Darwinium mapattribute (like model scores) can be parsed in a template by looping the keys and values.
{
{% for key, value in outcome['CHAMPION'].models.score | items %}
"{{ key }}": "{{ value }}"
{%- if not loop.last %},{%- endif %}
{% endfor %}
}
Can use dictsort filter instead if wanting to order entries by key
Math
The following mathematic operations are supported:
| Operator | Description | Example | Result |
| + | Addition of two numbers | {{ 1 + 3 }} | 4 |
| - | Subtract second number from the first | {{ 6-5 }} | 1 |
| / | Division of two numbers | {{ 1 / 2 }} | 0.5 |
| // | Integer division of two numbers | {{ 5 / 3 }} | 1 |
| % | Modulo (remainder) of two numbers | {{ 11 % 4 }} | 4 |
| * | Multiple the first number with the second | {{ 55 * 2 }} | 110 |
| ** | Raise the first number to the power of the second number | {{ 2 ** 3 }} | 8 |
Comparisons
| Operator | Description | Example | Result |
| == | Compares two operands for equality | 45 == 45 | true |
| != | Compares two operands for inequality | 21 != 201 | true |
| > | Evaluates if the first operand is greater than the second operand | 500 > 1000 | false |
| >= | Evaluates if the first operand is greater or equal than the second operand | 40 >= 50 | false |
| < | Evaluates if the first operand is less than the second operand | 20 < 200 | true |
| <= | Evaluates if the first operand is less or equal to the second operand | 33 <= 10 | false |
Logic
Boolean logic can be combined using logic operators. This is especially useful for if statements.
| Operator | Description | Example | Result |
| and | Return true if both the first and second operand are true | 45 != 105 and 23 < 100 | true |
| or | Return true if either the first or the second operand are true | 1 ==2 or 5 == 6 | false |
| not | negate a statement | not 1 == 2 | true |
| (expression) | Group a logic expression | (45 != 105 and 23 < 100) or 1 == 2 | true |
Other Operators
| Operator | Description | Example | Result | |
| is | is not | Performs a Test | {{ ( 45 is odd ) }} | True |
| in | not in | Performs a containment check on a list or map | 'some_value' is not in my_list | true |
| | | Applies a Filter | {{ 'DONT YELL AT ME' | lower }} | 'dont yell at me' | |
| ~ | Converts all operands into strings an concatenates them | 'Happy' ~ ' ' ~ 'Birthday' | 'Happy Birthday' | |
| () | Call a Callable | |||
| . | [] | Get an attribute on an object | ||
Tests
| Test | Description | Example | Result |
| defined | Test if a value is defined | nope_no_way is defined | false |
| endingwith | Test if a value ends with the supplied string | 'Tuesday' is endingwith 'day' | true |
| even | Test if a value is even | 44 is even | true |
| mapping | Test if a value is a mapping | {'hey' : 'joe' } is mapping | true |
| number | Test if a value is a number | 'boom' is number | false |
| odd | Test if a value is odd | 10 is odd | false |
| sequence | Test if a value is a sequence (list) | [23,24,25] is sequence | true |
| startingwith | Test if a value starts with a supplied string | 'BigBang' is startingwith 'Bang' | false |
| string | Test if a value is a string | 'my string' is string | true |
| undefined | Test if a value is not defined | nope_no_way is undefined | true |
Filters
A basic example of filters is using the Base64Encode filter 'b64encode' to create a basic HTTP Authentication header string as follows:
Basic {{ custom.general_purpose['user'] ~ ':' ~ custom.general_purpose['pword'] | b64encode }}| Operator | Description | Example | Result |
| abs | Return the absolute value of a number | {{ -11.22 | abs }} | 11.22 |
| b64encode | Base64 encode a string | {{ 'test1234' | b64encode }} | dGVzdDEyMzQ= |
| b64decode | Base64 decode a string | {{ 'dGVzdDEyMzQ=' | b64decode }} | test1234 |
| batch | Batch items | ||
| bool | Converts the value into a boolean | ||
| contains | Check if a string contains a sub-string | {{ 'Mickey Mouse' | contains('Mouse') }} | true |
| default | If the value is undefined then return the supplied default value otherwise return the value | {{ nope_no_way | 'this is my default' }} | this is my default |
| dictsort | {{ {"zz":"top","aardvark":"ant","middle":"earth"} | dictsort}} | [('aardvark', 'ant'), ('middle', 'earth'), ('zz', 'top')] | |
| escape | HTML escape a string | {{ '42 >= the meaning of life' | escape }} | 42 >= the meaning of life |
| first | Returns the first item from a list value | {{ ['aa','bb','ccc'] | last }} | aa |
| items | Returns a list of pairs (items) from a mapping | ||
| join | Joins a sequence by a character | ||
| last | Returns the last item from a list value | {{ ['aa','bb','ccc'] | last }} | ccc |
| length | Returns the length of a value | {{ ['aa','bb','ccc'] | length }} | 3 |
| list | Converts the input value into list | ||
| lower | Convert string into lowercase | {{ 'DONT YELL AT ME' | lower }} | don't yell at me |
| replace | Perform string replace | {{ 'a b c' | replace('b','BEE') }} | aBEE c |
| reverse | Reverse a list or string | {{ 'Reverse' | reverse }} | esreveR |
| round | Round the number to the supplied precision | {{ 12.23456 | round(3) }} | 12.235 |
| safe | Marks a value as safe. This converts it into a string. | ||
| slice | Slice an iterable value and return a list of lists containing those items | ||
| split | Split a string into items using a delimiter | {{ "Hello,World" | split(",") }} | ['Hello','World'] |
| strftime | Format a time value into a string using a formatting string | {{ timestamp | strftime('%Y-%m-%d %H:%M:%S') }} | 2022-07-01 14:22:39 |
| title | Converts a value to title case | {{ "attack of the killer b's" | title }} | Attack Of The Killer B's |
| trim | Trim leading and trailing whitespace from a value | {{ ' hey ' | trim }} | hey |
| upper | Convert string into uppercase | {{ 'stop whispering' }} | STOP WHISPERING |
| urlencode | URL encode the supplied string | {{ 'hello! from space' | urlencode }} | hello%21%20from%20space |