summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/00_syntax/18_if_condition
blob: 9e02767a253d654cc2c846b66f4d617b7f28697b (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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.


An if-condition using the special "elif" keyword in alternative syntax mode:
Variable x was set to five.


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 %}


An if-condition using the special "elif" keyword in alternative syntax mode:
{% if (x == 0): -%}
Variable x was set to zero.
{% elif (x == 1): -%}
Variable x was set to one.
{% elif (x == 5): -%}
Variable x was set to five.
{% 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 --