1 When invoked with a file path argment, the `render()` function executes 2 the specified path as ucode script, optionally setting a different 3 execution scope for the invoked file, and captures the produced output 4 in a string. 5 6 If the specified path is relative, it is treated as being relative to the 7 source file currently being executed or the current working directory in 8 case the interpreter executes code from stdin or a command line argument. 9 10 Throws an exception if the given path value is not a string. 11 12 Throws an exception if a scope argument is specified and not a valid object. 13 14 Throws an exception if the given path could not be found or opened. 15 16 Throws an exception if the given file could not be compiled. 17 18 When invoked with a function value, `render()` invokes the function, passes 19 all remaining arugments to it and captures any produces output in a string. 20 21 Returns a string containing the captured output of the executed file or 22 function. 23 24 -- Testcase -- 25 {% 26 let real_printf = printf; 27 28 printf("%.J\n", [ 29 // include by relative path 30 render("files/include.uc"), 31 32 // include by absolute path 33 render(TESTFILES_PATH + "/include.uc"), 34 35 // include with overridden scope 36 render("files/include.uc", { 37 printf: function(...args) { 38 real_printf("This is the wrapped printf() getting called!\n"); 39 40 return real_printf(...args); 41 } 42 }), 43 44 // include with isolated scope 45 render("files/include.uc", proto({ 46 printf: function(...args) { 47 real_printf("This is the wrapped printf() getting called!\n"); 48 49 return real_printf(...args); 50 } 51 }, {})) 52 ]); 53 %} 54 -- End -- 55 56 -- File include.uc -- 57 {% 58 printf("This is the include file running! Can I access the global env? %s\n", 59 REQUIRE_SEARCH_PATH ? "Yes!" : "No."); 60 %} 61 -- End -- 62 63 -- Expect stdout -- 64 [ 65 "This is the include file running! Can I access the global env? Yes!\n", 66 "This is the include file running! Can I access the global env? Yes!\n", 67 "This is the wrapped printf() getting called!\nThis is the include file running! Can I access the global env? Yes!\n", 68 "This is the wrapped printf() getting called!\nThis is the include file running! Can I access the global env? No.\n" 69 ] 70 -- End -- 71 72 73 An invalid path value triggers an exception. 74 75 -- Testcase -- 76 {% 77 render(true); 78 %} 79 -- End -- 80 81 -- Expect stderr -- 82 Type error: Passed filename is not a string 83 In line 2, byte 13: 84 85 ` render(true);` 86 Near here -----^ 87 88 89 -- End -- 90 91 92 An invalid scope value triggers an exception. 93 94 -- Testcase -- 95 {% 96 render("test", true); 97 %} 98 -- End -- 99 100 -- Expect stderr -- 101 Type error: Passed scope value is not an object 102 In line 2, byte 21: 103 104 ` render("test", true);` 105 Near here -------------^ 106 107 108 -- End -- 109 110 111 A not found file triggers an exception. 112 113 -- Testcase -- 114 {% 115 render("files/doesnotexist.uc"); 116 %} 117 -- End -- 118 119 -- Expect stderr -- 120 Runtime error: Include file not found 121 In line 2, byte 32: 122 123 ` render("files/doesnotexist.uc");` 124 Near here ------------------------^ 125 126 127 -- End -- 128 129 130 A compilation error in the file triggers an exception. 131 132 -- Testcase -- 133 {% 134 render("files/broken.uc"); 135 %} 136 -- End -- 137 138 -- File broken.uc -- 139 {% 140 // Unclosed object to force syntax error 141 return { 142 %} 143 -- End -- 144 145 -- Expect stderr -- 146 Runtime error: Unable to compile source file './files/broken.uc': 147 148 | Syntax error: Expecting label 149 | In line 4, byte 1: 150 | 151 | ` return {` 152 | Near here --^ 153 154 In line 2, byte 26: 155 156 ` render("files/broken.uc");` 157 Near here ------------------^ 158 159 160 -- End -- 161 162 163 Rendering a function value will capture it's output. 164 165 -- Testcase -- 166 {% 167 name = "world"; 168 169 printf("%.J\n", [ 170 render(print, "Test"), 171 render(loadstring("Hello, {{ name }}!")), 172 render(function(name) { 173 include("files/greeting.uc", { name }) 174 }, "Bob") 175 ]); 176 %} 177 -- End -- 178 179 -- File greeting.uc -- 180 Hello, {{ name }} 181 -- End -- 182 183 -- Expect stdout -- 184 [ 185 "Test", 186 "Hello, world!", 187 "Hello, Bob\n" 188 ] 189 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt