Intro
The SovLabs Template Engine enables injection of values for vRA Properties into the SovLabs profiles and the resulting build processes. It can combine static text with vRA custom properties and provides constructs for flow control, logic, iteration and transformation to provide a powerful layer of dynamically controlled metadata which simplifies the management of your configurations in vRA. No need to create multiple configurations to accommodate different hard-coded values for fields. With the SovLabs Template Engine, values can be decided on the fly based on selections at request time or other properties set on other components in vRA, such as custom properties on Business Groups, Reservations, and vCenter endpoints.
When you see a reference to a property enclosed in double curly brackets in the documentation for the modules, e.g. {{ my_propertyname }}
,
that implies use of the SovLabs Template engine to transform that property and use its resulting value in that place.
In SovLabs configurations in general, most fields are "templatable", which means you can substitute a valid Template Engine template in place of a hard-coded value.
Not only can you template many fields inside the SovLabs configurations, you can also use a template to set the value of a SovLabs Custom Property.
If substitution of vRA properties into the SovLabs profiles sounds powerful, this next bit is a game changer!
The Template engine syntax contains filters such as substring
, upcase
, join
, replace
and Tags for logic operations such as if/else
, case statements
, for
loops
and many more to not only inject the vRA properties into the SovLabs integrations, but to transform the properties into a layer of business logic between vRA and the SovLabs modules.
To see all the functions available in the SovLabs Template Engine, see below.
The SovLabs Template Engine is organized into Template Objects, Tags and Filters.
Outputs
Outputs render an expression. Outputs and variable names are denoted by double curly braces: {{
and }}
-
foobar = "Hello world"
{{ foobar }}
-
Hello world
The SovLabs Template Engine renders the content of the object foobar
, and that object contains the output Hello world
Tags
Tags create the logic and control flow for templates. Tags are denoted by curly braces and percent signs: {%
and %}
The markup used in tags do not produce visible text. Tags allows one to assign variables and create conditions and loops
-
user.name = 'John Doe'
{% if user.name == 'John Smith' %} Hello John Smith {% else %} Hello stranger {% endif %}
-
Hello stranger
Or for a more SovLabs-specific example, you could write a template to pick the correct SovLabs IPAM profile based on a vRA property called “Environment” instead of hard-coding the Custom Property SovLabs_IPAMProfile_nic0
to a specific profile.
You would create the custom property SovLabs_IPAMProfile_nic0
on your vRA blueprint and put the template in the Value field:
-
Environment = "production"
{% if Environment == 'production' %}IPAM_profile_prod{% elsif Environment == 'development' %}IPAM_profile_development{% elsif Environment == 'QA' %}IPAM_profile_QA{% else %}IPAM_profile_default{% endif %}
-
IPAM_profile_prod
Keep in mind that spaces and other characters show up as literals in the output, so if your template changes like this:
-
Environment = "production"
{% if Environment == 'production' %}X_ IPAM_profile_prod YY{% elsif Environment == 'development' %}IPAM_profile_development{% elsif Environment == 'QA' %}IPAM_profile_QA{% else %}IPAM_profile_default{% endif %}
-
X_ IPAM_profile_prod YY
Filters
Filters change the rendering of the Output. Filters are used within an Output and are separated by a pipe symbol |
-
{{ 'Hello' | append: ' world' }}
-
Hello world
Multiple filters can be used on one output. The filters are applied from left to right.
-
{{ 'world!' | capitalize | prepend: 'Hello' }}
-
Hello World!
Types
SovLabs Template Engine can have one of 5 types:
String
Declare a string by wrapping a variable’s value in single or double quotes
{% assign my_string = "Hello world" %}
Number
Numbers include floats and integers
{% assign my_int = 25 %}
{% assign my_float = 39.756 %}
Boolean
Booleans are either true
or false
. No quotations are necessary when declaring a boolean
{% assign foo = true %}
{% assign bar = false %}
Nil
Nil is a special empty value that is returned when the output has no results. It is not a string with the characters “nil”.
Nil is treated as false
in the conditions of if
blocks and other tags that check the truthfulness of a statement.
In the following example, if the user does not exist (that is, user
returns nil
), SovLabs Template Engine will not print the greeting:
{% if user %}
Hello {{ user.name }}!
{% endif %}
Tags or outputs that return nil
will not print anything to the page.
-
The current user is {{ user.name }}
-
The current user is
Array
Arrays hold lists of variables of any type
Accessing items in arrays
To access all the items in an array, loop through each item in the array using iterations (e.g. for
tag)
{% for user in users %}
{{ user }}
{% endfor %}
Accessing specific items in arrays
Use square bracket [
and ]
notation to access a specific item in an array. Array indexing starts at zero.
{{ users[0] }}
Initializing arrays
The SovLabs Template Engine is unable to initialize arrays.
However, use the split filer to break a string into an array of substrings