Variables
Postways allow for very powerful variables to be inserted into templates. Variables can range from simple pieces of text to complex data structures.
Expressions
Let's start with a basic expression:
{{ foo }}
In the above example, foo
is the variable and the double curly braces {{
and }}
mean that the value of foo
should be displayed, i.e. outputted. Variables can be nested inside objects. E.g. if foo
was an attribute of an object called bar
then we could write:
{{ bar.foo }}
Most of the time a variable will be passed into the template but if we want we can define variables inside the template:
{% set foo = 'something' %}
The above is a statement and a statement uses the {%
and %}
rather than the double curly braces. In the above example we have declared the variable foo
with the value something
. We can also define an object this way:
{% set foo = {bar:"something"} %}
Defining an object is basically the same syntax as JSON (JavaScript Object Notation) that you might already be familiar with. We can also define an array:
{% set foo = ["item1", "item2"] %}
Now foo
is an array (list) containing two elements, in this case two strings. The elements of an array can also be objects.
Postways templates allow you to create control structures, such as "loops":
{% for i in range(0, 3) %}
{{ i }}
{% endfor %}
In the above example we create a simple for
loop over a array (list) of numbers that is created by the range
function. Each interation of the loop the value is stored in the i
variable which is outputted.
A more common scenario though would be to interate over a list of objects. In the following example we have a list of "items". Each item in the list has an attribute called foo
which we output:
{% for item in items %}
{{ item.foo }}
{% endfor %}
Besides loops, an if
statement is probably the most used control structure:
{% if items|length > 0 %}
{% for item in items %}
{{ item.foo }}
{% endfor %}
{% else %}
No items found
{% endif %}
In the above example we have the exact same loop as we has before, however, this time we first check if the number of items in the list is greater then zero by using the length
function. If so, we loop over the items otherwise we output the text "No items found".
With the if
statement you can also test if something does, or does not, exist. To do this we use defined
. E.g. to test if foo
is defined we do:
{% if foo is defined %}
...
{% endif %}
However, we can also test the opposite:
{% if foo is not defined %}
...
{% endif %}
Functions
Functions allow you to do a great deal with your variables. Great examples are the formatting of time and money. E.g. rather than you application sending a fully formatted time stamp as a variable to Postways, your application would send the time as an Unix timestamp (seconds since Epoch) in UTC (timezone +0). By doing this, it would allow you to format the timestamp in the template rather than in the source of your application.
To call a function we use the |
(pipe) symbol were the |
is placed between the variable and the function. It basically means we want to pass the variable as the first parameter into the function.
Time & Date
By using the "now"
and the date
function we can output the current time:
{{ "now"|date }}
The above would output something like "July 2, 2017 06:41" (depending on the current time). However, we can change the format of how the time is displayed:
{{ "now"|date('d/m/Y H:i') }}
The above would output something like "02/07/2017 06:42". The problem though, is that the time is in UTC. We can fix this by specifying a timezone:
{{ "now"|date('d/m/Y H:i', "Australia/Sydney") }}
Which results in "02/07/2017 16:43". A 10 hour time difference.
Instead of using "now"
, use an epoch time instead which is provided through a variable:
{{ myTimeStamp|date }}
Everthing else works the same.
Text Manipulation
We can change text and variables to upper and lowercase with the upper
and lower
functions respectively:
{{ foo|upper }}
{{ foo|lower }}
And we can "glue" strings together, i.e. concatinate:
{% set greeting = 'Hello' %}
{% set name = 'Postways' %}
{{ greeting ~' '~ name|upper }}
First we defined two variables; greeting
and name
. Then we concatinated them together (seperated by a space) while "upper"-casing the name
variable.