1 module temple.tests.capture; 2 3 import temple.tests.common; 4 5 unittest 6 { 7 // test captures 8 alias render = Temple!q{ 9 <% auto a = capture(() { %> 10 This is captured in A 11 <% }); %> 12 <% auto b = capture(() { %> 13 This is captured in B 14 <% }); %> 15 16 B said: "<%= b %>" 17 A said: "<%= a %>" 18 }; 19 20 auto accum = new AppenderOutputStream; 21 render(accum); 22 23 assert(isSameRender(accum.data, ` 24 B said: "This is captured in B" 25 A said: "This is captured in A" 26 `)); 27 } 28 29 unittest 30 { 31 // Nested captures 32 alias render = Temple!q{ 33 <% auto outer = capture(() { %> 34 Outer, first 35 <% auto inner = capture(() { %> 36 Inner, first 37 <% }); %> 38 Outer, second 39 40 <%= inner %> 41 <% }); %> 42 43 <%= outer %> 44 }; 45 46 auto accum = new AppenderOutputStream; 47 render(accum); 48 49 assert(isSameRender(accum.data, ` 50 Outer, first 51 Outer, second 52 Inner, first 53 `)); 54 } 55 56 unittest 57 { 58 alias render = TempleFile!"test8_building_helpers.emd"; 59 assert(isSameRender(templeToString(&render), readText("test/test8_building_helpers.emd.txt"))); 60 } 61 62 unittest 63 { 64 alias render = Temple!q{ 65 <%= capture(() { %> 66 directly printed 67 68 <% auto a = capture(() { %> 69 a, captured 70 <% }); %> 71 <% auto b = capture(() { %> 72 b, captured 73 <% }); %> 74 75 <%= a %> 76 <%= capture(() { %> 77 directly printed from a nested capture 78 <% }); %> 79 <%= b %> 80 81 <% }); %> 82 }; 83 84 auto accum = new AppenderOutputStream; 85 render(accum); 86 87 assert(isSameRender(accum.data, ` 88 directly printed 89 a, captured 90 directly printed from a nested capture 91 b, captured`)); 92 } 93 94 /** 95 * Test CTFE compatibility 96 */ 97 unittest 98 { 99 alias render = Temple!q{ <%= "foo" %> }; 100 const result = templeToString(&render); 101 static assert(isSameRender(result, "foo")); 102 } 103 104 unittest 105 { 106 alias render = Temple!q{ 107 <% if(true) { %> 108 Bort 109 <% } else { %> 110 No bort! 111 <% } %> 112 113 <% auto a = capture(() { %> 114 inside a capture block 115 <% }); %> 116 117 Before capture 118 <%= a %> 119 After capture 120 }; 121 122 const result = templeToString(&render); 123 static assert(isSameRender(result, ` 124 Bort 125 Before capture 126 inside a capture block 127 After capture 128 `)); 129 }