1 The ucode language supports ES6 template literals for easy interpolation 2 of expression results into strings. 3 4 5 1. Simple template literals are equivalent to strings. 6 7 -- Testcase -- 8 {{ `foo` === 'foo' }} 9 -- End -- 10 11 -- Expect stdout -- 12 true 13 -- End -- 14 15 16 2. Template literals may embed expressions using `${...}` placeholder notation. 17 18 -- Testcase -- 19 {% 20 let x = 2; 21 let y = 4; 22 23 print(`The result of ${x} * ${y} is ${x * y}\n`); 24 %} 25 -- End -- 26 27 -- Expect stdout -- 28 The result of 2 * 4 is 8 29 -- End -- 30 31 32 3. Template literals may be nested. 33 34 -- Testcase -- 35 {% 36 let isFoo = false; 37 let isBar = true; 38 39 print(`Foo is ${isFoo} and ${isBar ? `bar is ${isBar}` : `nothing else`}!\n`); 40 %} 41 -- End -- 42 43 -- Expect stdout -- 44 Foo is false and bar is true! 45 -- End -- 46 47 48 4. Placeholder expression results are implicitly stringified. 49 50 -- Testcase -- 51 {% 52 let o1 = { foo: true }; 53 let o2 = proto({ color: "red" }, { tostring: function() { return `I am a ${this.color} object` } }); 54 55 print(`The first object is ${o1} and the second says "${o2}".\n`); 56 %} 57 -- End -- 58 59 -- Expect stdout -- 60 The first object is { "foo": true } and the second says "I am a red object". 61 -- End -- 62 63 64 5. Escaping either `$` or `{` prevents interpolation as placeholder, sole `$` 65 characters bear no special meaning. 66 67 -- Testcase -- 68 {% 69 printf("%.J\n", [ 70 `foo \${bar} baz`, 71 `foo $\{bar} baz`, 72 `foo $bar baz` 73 ]); 74 %} 75 -- End -- 76 77 -- Expect stdout -- 78 [ 79 "foo ${bar} baz", 80 "foo ${bar} baz", 81 "foo $bar baz" 82 ] 83 -- End -- 84 85 86 6. Unterminated placeholder expressions are a synatax error. 87 88 -- Testcase -- 89 {{ 90 `foo ${ bar` 91 }} 92 -- End -- 93 94 -- Expect stderr -- 95 Syntax error: Unterminated string 96 In line 2, byte 13: 97 98 ` `foo ${ bar`` 99 Near here -----^ 100 101 102 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt