summaryrefslogtreecommitdiffhomepage
path: root/tests/00_syntax/18_if_condition
blob: 01484664d233b905c6ca29c90fd793e1c3830af5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
Utpl implements C-style if/else conditions and ?: ternary statements.

Like with for- and while-loops, an alternative syntax form suitable
for template blocks is supported.


-- Expect stdout --
This should print "one":
one

This should print "two":
two

Multiple conditions can be used by chaining if/else statements:
three

If the conditional block consists of only one statement, the curly
braces may be omitted:
two

An if-condition using the alternative syntax:
Variable x has another value.


Ternary expressions function similar to if/else statements but
only allow for a single expression in the true and false branches:
Variable x is one
-- End --

-- Testcase --
This should print "one":
{%
	x = 0;

	if (x == 0) {
		print("one");
	}
	else {
		print("two");
	}
%}

This should print "two":
{%
	x = 1;

	if (x == 0) {
		print("one");
	}
	else {
		print("two");
	}
%}

Multiple conditions can be used by chaining if/else statements:
{%
	x = 2;

	if (x == 0) {
		print("one");
	}
	else if (x == 1) {
		print("two");
	}
	else if (x == 2) {
		print("three");
	}
	else {
		print("four");
	}
%}

If the conditional block consists of only one statement, the curly
braces may be omitted:
{%
	x = 5;

	if (x == 0)
		print("one");
	else
		print("two");
%}

An if-condition using the alternative syntax:
{% if (x == 1): -%}
Variable x was set to one.
{% else -%}
Variable x has another value.
{% endif %}

Ternary expressions function similar to if/else statements but
only allow for a single expression in the true and false branches:
{%
	x = 1;
	s = (x == 1) ? "Variable x is one" : "Variable x has another value";

	print(s);
%}
-- End --