Documentation Index

Fetch the complete documentation index at: https://docs.darwinium.com/llms.txt

Use this file to discover all available pages before exploring further.

Template Markup

Prev Next

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.

Trailing new lines in Template files
Like Jinja2, Darwinium File Templating will remove one trailing new line from the end of the file automatically. This allows templates to produce a consistent output no matter if the file editor adds a trailing new line or not. In order to force a trailing new line, simply add an additional new line at the end of the template file.

 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!
Space characters inside expressions
Space characters inside expressions are not output, unless they are inside a String Literal.
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 Smith

Example: 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:

OperatorDescriptionExampleResult
+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

OperatorDescriptionExampleResult
==Compares two operands for equality45 == 45true
!=Compares two operands for inequality21 != 201true
>Evaluates if the first operand is greater than the second operand500 > 1000false
>=Evaluates if the first operand is greater or equal than the second operand40 >= 50false
<Evaluates if the first operand is less than the second operand20 < 200true
<=Evaluates if the first operand is less or equal to the second operand33 <= 10false

Logic

Boolean logic can be combined using logic operators. This is especially useful for if statements.

OperatorDescriptionExampleResult
andReturn true if both the first and second operand are true45 != 105 and 23 < 100true
orReturn true if either the first or the second operand are true1 ==2 or 5 == 6 false
notnegate a statementnot 1 == 2true
(expression)Group a logic expression(45 != 105 and 23 < 100) or 1 == 2true

Other Operators

OperatorDescriptionExampleResult
isis notPerforms a Test {{ ( 45 is odd ) }}True
innot inPerforms a containment check on a list or map'some_value' is not in my_listtrue
|
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

TestDescriptionExampleResult
definedTest if a value is definednope_no_way is definedfalse
endingwithTest if a value ends with the supplied string'Tuesday' is endingwith 'day'true
evenTest if a value is even44 is eventrue
mappingTest if a value is a mapping{'hey' : 'joe' } is mappingtrue
numberTest if a value is a number'boom' is numberfalse
oddTest if a value is odd10 is oddfalse
sequenceTest if a value is a sequence (list)[23,24,25] is sequencetrue
startingwithTest if a value starts with a supplied string'BigBang' is startingwith 'Bang'false
stringTest if a value is a string'my string' is stringtrue
undefinedTest if a value is not definednope_no_way is undefinedtrue

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 }}
OperatorDescriptionExampleResult
absReturn the absolute value of a number{{ -11.22 | abs }}11.22
b64encodeBase64 encode a string{{ 'test1234' | b64encode }} dGVzdDEyMzQ=
b64decodeBase64 decode a string{{ 'dGVzdDEyMzQ=' | b64decode }}test1234
batchBatch items

boolConverts the value into a boolean

containsCheck if a string contains a sub-string{{ 'Mickey Mouse' | contains('Mouse') }}true
defaultIf 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')]
escapeHTML escape a string{{ '42 >= the meaning of life' | escape }}42 &gt;= the meaning of life
firstReturns the first item from a list value{{ ['aa','bb','ccc'] | last }}aa
itemsReturns a list of pairs (items) from a mapping

joinJoins a sequence by a character

lastReturns the last item from a list value{{ ['aa','bb','ccc'] | last }}ccc
lengthReturns the length of a value{{ ['aa','bb','ccc'] | length }}3
listConverts the input value into list

lowerConvert string into lowercase{{ 'DONT YELL AT ME' | lower }}don't yell at me
replacePerform string replace{{ 'a b c' | replace('b','BEE') }}aBEE c
reverseReverse a list or string{{ 'Reverse' | reverse }}esreveR
roundRound the number to the supplied precision{{ 12.23456 | round(3) }}12.235
safeMarks a value as safe. This converts it into a string.

sliceSlice an iterable value and return a list of lists containing those items

splitSplit a string into items using a delimiter{{ "Hello,World" | split(",") }}['Hello','World']
strftimeFormat a time value into a string using a formatting string{{ timestamp | strftime('%Y-%m-%d %H:%M:%S') }}2022-07-01 14:22:39
titleConverts a value to title case{{ "attack of the killer b's" | title }}Attack Of The Killer B's
trimTrim leading and trailing whitespace from a value

{{ '

      hey   ' | trim }}

hey
upperConvert string into uppercase{{ 'stop whispering' }}STOP WHISPERING
urlencodeURL encode the supplied string{{ 'hello! from space' | urlencode }}hello%21%20from%20space