• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/00_syntax/18_if_condition

  1 Ucode implements C-style if/else conditions and ?: ternary statements.
  2 
  3 Like with for- and while-loops, an alternative syntax form suitable
  4 for template blocks is supported.
  5 
  6 
  7 -- Expect stdout --
  8 This should print "one":
  9 one
 10 
 11 This should print "two":
 12 two
 13 
 14 Multiple conditions can be used by chaining if/else statements:
 15 three
 16 
 17 If the conditional block consists of only one statement, the curly
 18 braces may be omitted:
 19 two
 20 
 21 An if-condition using the alternative syntax:
 22 Variable x has another value.
 23 
 24 
 25 An if-condition using the special "elif" keyword in alternative syntax mode:
 26 Variable x was set to five.
 27 
 28 
 29 Ternary expressions function similar to if/else statements but
 30 only allow for a single expression in the true and false branches:
 31 Variable x is one
 32 -- End --
 33 
 34 -- Testcase --
 35 This should print "one":
 36 {%
 37         x = 0;
 38 
 39         if (x == 0) {
 40                 print("one");
 41         }
 42         else {
 43                 print("two");
 44         }
 45 %}
 46 
 47 
 48 This should print "two":
 49 {%
 50         x = 1;
 51 
 52         if (x == 0) {
 53                 print("one");
 54         }
 55         else {
 56                 print("two");
 57         }
 58 %}
 59 
 60 
 61 Multiple conditions can be used by chaining if/else statements:
 62 {%
 63         x = 2;
 64 
 65         if (x == 0) {
 66                 print("one");
 67         }
 68         else if (x == 1) {
 69                 print("two");
 70         }
 71         else if (x == 2) {
 72                 print("three");
 73         }
 74         else {
 75                 print("four");
 76         }
 77 %}
 78 
 79 
 80 If the conditional block consists of only one statement, the curly
 81 braces may be omitted:
 82 {%
 83         x = 5;
 84 
 85         if (x == 0)
 86                 print("one");
 87         else
 88                 print("two");
 89 %}
 90 
 91 
 92 An if-condition using the alternative syntax:
 93 {% if (x == 1): -%}
 94 Variable x was set to one.
 95 {% else -%}
 96 Variable x has another value.
 97 {% endif %}
 98 
 99 
100 An if-condition using the special "elif" keyword in alternative syntax mode:
101 {% if (x == 0): -%}
102 Variable x was set to zero.
103 {% elif (x == 1): -%}
104 Variable x was set to one.
105 {% elif (x == 5): -%}
106 Variable x was set to five.
107 {% else -%}
108 Variable x has another value.
109 {% endif %}
110 
111 
112 Ternary expressions function similar to if/else statements but
113 only allow for a single expression in the true and false branches:
114 {%
115         x = 1;
116         s = (x == 1) ? "Variable x is one" : "Variable x has another value";
117 
118         print(s);
119 %}
120 
121 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt