Skip to content

Swig

As mentioned in the concept explanation we make use of Swig to render the templates on the Web Interface. We introduce in this page the content specific to Swig. The reader is also referred to Swig official documentation for further reading 1.

Javascript Native Prototypes

All Javascript-related prototypes such as Array and Object 2 are supported by Swig, as long as the function does not require a callback (function) as one of its arguments. For example Array.prototype.find() is not supported by Swig as it needs a callback, however it can be implemented by pure templating features as below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{% set elements = [
  {"id": 0, "value": "Si"}, 
  {"id": 1, "value": "Ge"}
] %}
{% set element = "" %}
{% set element_found = false %}
{% for element_ in elements %}
  {% if not element_found and element_["id"] == 1 %}
    {% set element = element_ %}
    {% set element_found = true %}
  {% endif %}
{% endfor %}
element = {{ element["value"] }}

Specific Statements

Spaceless

{% spaceless %} statement ensures that the text is rendered with no extra white spaces or empty lines added to it, and has to be terminated by {% endspaceless %}.