| 1 | #!/usr/bin/perl |
|---|
| 2 | # $Id: 07-builder.t 3022 2008-09-03 20:16:30Z bchoate $ |
|---|
| 3 | |
|---|
| 4 | use strict; |
|---|
| 5 | use lib qw( t t/lib lib extlib ); |
|---|
| 6 | use Data::Dumper; |
|---|
| 7 | |
|---|
| 8 | use MT::Test; |
|---|
| 9 | |
|---|
| 10 | use Test::More tests => 142; |
|---|
| 11 | |
|---|
| 12 | use MT; |
|---|
| 13 | |
|---|
| 14 | my $mt = MT->new; |
|---|
| 15 | |
|---|
| 16 | my($tokens, $out); |
|---|
| 17 | |
|---|
| 18 | use MT::Builder; |
|---|
| 19 | my $builder = MT::Builder->new; |
|---|
| 20 | ok($builder, "Builder constructed okay"); |
|---|
| 21 | |
|---|
| 22 | my $ctx = My::Context->new; |
|---|
| 23 | ok($ctx, "Context constructed okay"); |
|---|
| 24 | |
|---|
| 25 | diag("Testing compilation of an empty template"); |
|---|
| 26 | $tokens = $builder->compile($ctx, ''); |
|---|
| 27 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 28 | ok($tokens, "Compiled empty template"); |
|---|
| 29 | ok(ref($tokens) eq 'ARRAY', "Empty template produced token list"); |
|---|
| 30 | ok(!@$tokens, "Token list is empty, which is okay"); |
|---|
| 31 | is($builder->build($ctx, $tokens), '', "Builds to an empty string"); |
|---|
| 32 | |
|---|
| 33 | diag("Testing compilation of a pure-text template (no tags)"); |
|---|
| 34 | $tokens = $builder->compile($ctx, 'justified and ancient'); |
|---|
| 35 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 36 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields tokens"); |
|---|
| 37 | ok(@$tokens == 1, "Created one token"); |
|---|
| 38 | ok($tokens->[0][0] eq 'TEXT', "Token is textual"); |
|---|
| 39 | ok($tokens->[0][1] eq 'justified and ancient', "Token text is what we expect"); |
|---|
| 40 | is($builder->build($ctx, $tokens), 'justified and ancient', "Builds to what we expect"); |
|---|
| 41 | |
|---|
| 42 | diag("Testing compilation of simple function tag"); |
|---|
| 43 | $tokens = $builder->compile($ctx, '<$MTFoo$>'); |
|---|
| 44 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 45 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields tokens"); |
|---|
| 46 | ok(@$tokens == 1, "Created one token"); |
|---|
| 47 | ok($tokens->[0][0] eq 'Foo', "Token is a tag token"); |
|---|
| 48 | ok(@{ $tokens->[0] } == 7, "Length of token object is 7 elements"); |
|---|
| 49 | ok(ref($tokens->[0][1]) eq 'HASH', "Element 1 of token object is a hashref"); |
|---|
| 50 | is(scalar keys %{ $tokens->[0][1] }, 0, "Has no attributes"); |
|---|
| 51 | is($builder->build($ctx, $tokens), 'foo', "Building produces expected result"); |
|---|
| 52 | |
|---|
| 53 | diag("Testing compilation of function tag with an attribute"); |
|---|
| 54 | $tokens = $builder->compile($ctx, '<$MTFoo no="1"$>'); |
|---|
| 55 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 56 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields tokens"); |
|---|
| 57 | ok(@$tokens == 1, "Created one token"); |
|---|
| 58 | ok(@{ $tokens->[0] } == 7, "Length of token object is 7 elements"); |
|---|
| 59 | ok($tokens->[0][0] eq 'Foo', "Token is a tag token"); |
|---|
| 60 | ok(ref($tokens->[0][1]) eq 'HASH', "Element 1 of token object is a hashref"); |
|---|
| 61 | is($tokens->[0][1]{no}, 1, "Attribute 'no' is equal to 1"); |
|---|
| 62 | is($builder->build($ctx, $tokens), 'no foo', "Building produces expected result"); |
|---|
| 63 | |
|---|
| 64 | diag("Testing compilation of function tag with multiple attributes"); |
|---|
| 65 | $tokens = $builder->compile($ctx, '<$MTFoo no="1" yes="foo bar"$>'); |
|---|
| 66 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 67 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields tokens"); |
|---|
| 68 | ok(@$tokens == 1, "Created one token"); |
|---|
| 69 | ok(@{ $tokens->[0] } == 7, "Length of token object is 7 elements"); |
|---|
| 70 | ok($tokens->[0][0] eq 'Foo', "Token is a tag token"); |
|---|
| 71 | ok(ref($tokens->[0][1]) eq 'HASH', "Element 1 of token object is a hashref"); |
|---|
| 72 | is($tokens->[0][1]{no}, 1, "Attribute 'no' is equal to 1"); |
|---|
| 73 | is($tokens->[0][1]{yes}, 'foo bar', "Attribute 'yes' is equal to 'foo bar'"); |
|---|
| 74 | is($builder->build($ctx, $tokens), 'no foo', "Building produces expected result"); |
|---|
| 75 | |
|---|
| 76 | diag("Testing compilation of function tag with attribute an inner single quote"); |
|---|
| 77 | $tokens = $builder->compile($ctx, '<$MTFoo yes="foo\'s bar"$>'); |
|---|
| 78 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 79 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields tokens"); |
|---|
| 80 | ok(@$tokens == 1, "Created one token"); |
|---|
| 81 | ok(@{ $tokens->[0] } == 7, "Length of token object is 7 elements"); |
|---|
| 82 | ok($tokens->[0][0] eq 'Foo', "Token is a tag token"); |
|---|
| 83 | ok(ref($tokens->[0][1]) eq 'HASH', "Element 1 of token object is a hashref"); |
|---|
| 84 | is($tokens->[0][1]{yes}, 'foo\'s bar', "Attribute 'yes' is equal to \"foo's bar\""); |
|---|
| 85 | |
|---|
| 86 | diag("Testing compilation of text + function tag"); |
|---|
| 87 | $tokens = $builder->compile($ctx, <<'TEXT'); |
|---|
| 88 | time to kick out the jams, motherfuckers |
|---|
| 89 | <$MTFoo$> |
|---|
| 90 | TEXT |
|---|
| 91 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 92 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields tokens"); |
|---|
| 93 | ok(@$tokens == 3, "Created 3 tokens"); |
|---|
| 94 | ok($tokens->[0][0] eq 'TEXT', "Token 1 is a text token"); |
|---|
| 95 | ok($tokens->[0][1] eq "time to kick out the jams, motherfuckers\n", "Text is expected value"); |
|---|
| 96 | ok($tokens->[1][0] eq 'Foo', "Token 2 is a tag token"); |
|---|
| 97 | ok($tokens->[2][0] eq 'TEXT', "Token 3 is a text token"); |
|---|
| 98 | ok($tokens->[2][1] eq "\n", "Text is expected value"); |
|---|
| 99 | is($builder->build($ctx, $tokens), |
|---|
| 100 | "time to kick out the jams, motherfuckers\nfoo\n", "Building produces expected result"); |
|---|
| 101 | is($builder->build($ctx, $tokens, { Foo => 0 }), |
|---|
| 102 | "time to kick out the jams, motherfuckers\n\n", "Building produces expected result, with conditional"); |
|---|
| 103 | |
|---|
| 104 | diag("Testing compilation failure for a block tag (no closing tag)"); |
|---|
| 105 | $tokens = $builder->compile($ctx, '<MTBars>'); |
|---|
| 106 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 107 | ok(!$tokens, "Compiling failed, as expected"); |
|---|
| 108 | ok($builder->errstr eq "<MTBars> with no </MTBars> on line 1.\n", "Compilation yielded proper error message"); |
|---|
| 109 | |
|---|
| 110 | # diag("Testing compilation failure for a nested block tag"); |
|---|
| 111 | # $tokens = $builder->compile($ctx, <<EOT); |
|---|
| 112 | # <MTBars> |
|---|
| 113 | # |
|---|
| 114 | # |
|---|
| 115 | # |
|---|
| 116 | # |
|---|
| 117 | # <MTBars> |
|---|
| 118 | # |
|---|
| 119 | # </MTBars> |
|---|
| 120 | # |
|---|
| 121 | # <MTBars> # ERROR IS HERE, LINE 10 |
|---|
| 122 | # |
|---|
| 123 | # <MTBars> |
|---|
| 124 | # |
|---|
| 125 | # </MTBars> |
|---|
| 126 | # EOT |
|---|
| 127 | # diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 128 | # ok(!$tokens, "Compiling failed, as expected"); |
|---|
| 129 | # ok($builder->errstr eq "<MTBars> with no </MTBars> on line 10.\n", "Compilation yielded proper error message"); |
|---|
| 130 | |
|---|
| 131 | diag("Testing compilation of a block tag with nothing in it"); |
|---|
| 132 | $tokens = $builder->compile($ctx, '<MTBars></MTBars>'); |
|---|
| 133 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 134 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields tokens"); |
|---|
| 135 | ok(@$tokens == 1, "Created 1 token"); |
|---|
| 136 | ok($tokens->[0][0] eq 'Bars', "Token is a tag token"); |
|---|
| 137 | ok(ref($tokens->[0][2]) eq 'ARRAY', "Token has child token array"); |
|---|
| 138 | ok(@{ $tokens->[0][2] } == 0, "Child token length is 0"); |
|---|
| 139 | is($builder->build($ctx, $tokens), 'Called without tokens!', "Building produces expected result"); |
|---|
| 140 | |
|---|
| 141 | diag("Testing compilation of a block tag wrapping plaintext"); |
|---|
| 142 | $tokens = $builder->compile($ctx, '<MTBars>foo</MTBars>'); |
|---|
| 143 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 144 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields tokens"); |
|---|
| 145 | ok(@$tokens == 1, "Created 1 token"); |
|---|
| 146 | ok($tokens->[0][0] eq 'Bars', "Token is a tag token"); |
|---|
| 147 | ok(ref($tokens->[0][2]) eq 'ARRAY', "Token has child token array"); |
|---|
| 148 | ok(@{ $tokens->[0][2] } == 1, "Child token length is 1"); |
|---|
| 149 | ok($tokens->[0][2][0][0] eq 'TEXT', "Child token is textual"); |
|---|
| 150 | ok($tokens->[0][2][0][1] eq 'foo', "Child token value is 'foo'"); |
|---|
| 151 | is($builder->build($ctx, $tokens), 'foofoo', "Building produces expected result"); |
|---|
| 152 | |
|---|
| 153 | diag("Testing compilation of a block tag wrapping plaintext + function tag"); |
|---|
| 154 | $tokens = $builder->compile($ctx, <<'TEXT'); |
|---|
| 155 | <MTBars> |
|---|
| 156 | foo:<$MTBarBaz$> |
|---|
| 157 | </MTBars> |
|---|
| 158 | TEXT |
|---|
| 159 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 160 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields tokens"); |
|---|
| 161 | ok(@$tokens == 2, "Created 2 tokens"); |
|---|
| 162 | ok($tokens->[0][0] eq 'Bars', "Token 1 is a tag token"); |
|---|
| 163 | ok(ref($tokens->[0][2]) eq 'ARRAY', "Token has child token array"); |
|---|
| 164 | ok(@{ $tokens->[0][2] } == 3, "Child token array length is 3"); |
|---|
| 165 | ok($tokens->[0][2][0][0] eq 'TEXT', "First child token is textual"); |
|---|
| 166 | ok($tokens->[0][2][0][1] eq "\nfoo:", "First child token text matches"); |
|---|
| 167 | ok($tokens->[0][2][1][0] eq 'BarBaz', "Second child token is a tag"); |
|---|
| 168 | ok($tokens->[0][2][2][0] eq 'TEXT', "Third child token is textual"); |
|---|
| 169 | ok($tokens->[0][2][2][1] eq "\n", "Third child token text matches"); |
|---|
| 170 | ok($tokens->[1][0] eq 'TEXT', "Second token is textual"); |
|---|
| 171 | ok($tokens->[1][1] eq "\n", "Second token text matches"); |
|---|
| 172 | is($builder->build($ctx, $tokens), "\nfoo:baz1\n\nfoo:baz2\n\n", "Building produces expected result"); |
|---|
| 173 | |
|---|
| 174 | $tokens = $builder->compile($ctx, |
|---|
| 175 | q[<$MTFoo regex="s/(\d+)/$1==0?'None':$1==1?'1 reply':$1.'replies'/e"$>]); |
|---|
| 176 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 177 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields tokens"); |
|---|
| 178 | ok(@$tokens == 1, "Created 1 token"); |
|---|
| 179 | ok(@{ $tokens->[0] } == 7, "Length of token object is 7 elements"); |
|---|
| 180 | ok($tokens->[0][0] eq 'Foo', "Token 1 is a tag token"); |
|---|
| 181 | ok(ref($tokens->[0][1]) eq 'HASH', "Token has an attribute hashref"); |
|---|
| 182 | is($tokens->[0][1]{regex}, q[s/(\d+)/$1==0?'None':$1==1?'1 reply':$1.'replies'/e], "'regex' attribute is set properly"); |
|---|
| 183 | |
|---|
| 184 | diag("Testing compilation of nesting a block tag"); |
|---|
| 185 | $tokens = $builder->compile($ctx, <<'TEXT'); |
|---|
| 186 | <MTBars> |
|---|
| 187 | Bars: |
|---|
| 188 | <MTBars>bar</MTBars> |
|---|
| 189 | </MTBars> |
|---|
| 190 | TEXT |
|---|
| 191 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 192 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields tokens"); |
|---|
| 193 | ok(@$tokens == 2, "Created 2 tokens"); |
|---|
| 194 | ok($tokens->[0][0] eq 'Bars', "Token 1 is a tag token"); |
|---|
| 195 | ok(ref($tokens->[0][2]) eq 'ARRAY', "Token 1 has subtokens"); |
|---|
| 196 | ok(@{ $tokens->[0][2] } == 3, "Subtoken length is 3"); |
|---|
| 197 | ok($tokens->[0][2][0][0] eq 'TEXT', "Subtoken 1 is textual"); |
|---|
| 198 | ok($tokens->[0][2][0][1] eq "\nBars:\n", "Subtoken 1 is expected value"); |
|---|
| 199 | ok($tokens->[0][2][1][0] eq 'Bars', "Subtoken 2 is a tag token"); |
|---|
| 200 | ok(ref($tokens->[0][2][1][2]) eq 'ARRAY', "Subtoken 2 contains subtokens"); |
|---|
| 201 | ok(@{ $tokens->[0][2][1][2] } == 1, "Subtoken 2 has 1 child token"); |
|---|
| 202 | ok($tokens->[0][2][1][2][0][0] eq 'TEXT', "Subtoken 2's child node is textual"); |
|---|
| 203 | ok($tokens->[0][2][1][2][0][1] eq 'bar', "Subtoken 2's child node is expected value"); |
|---|
| 204 | ok($tokens->[0][2][2][0] eq 'TEXT', "Subtoken 3 is textual"); |
|---|
| 205 | ok($tokens->[0][2][2][1] eq "\n", "Subtoken 3 is expected value"); |
|---|
| 206 | ok($tokens->[1][0] eq 'TEXT', "Token 2 is textual"); |
|---|
| 207 | ok($tokens->[1][1] eq "\n", "Token 2 is expected value"); |
|---|
| 208 | is($builder->build($ctx, $tokens), "\nBars:\nbarbar\n\nBars:\nbarbar\n\n", "Building produces expected result"); |
|---|
| 209 | |
|---|
| 210 | diag("Testing compilation of an empty block tag (singlet syntax)"); |
|---|
| 211 | $tokens = $builder->compile($ctx, "<MTBars/>"); |
|---|
| 212 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 213 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields tokens"); |
|---|
| 214 | ok(@$tokens == 1, "Created 1 token"); |
|---|
| 215 | ok($tokens->[0][0] eq 'Bars', "Token is a tag token"); |
|---|
| 216 | ok(!@{ $tokens->[0][2] || [] }, "Subtoken list is empty"); |
|---|
| 217 | is($builder->build($ctx, $tokens), 'Called without tokens!', "Building produces expected result"); |
|---|
| 218 | |
|---|
| 219 | diag("Testing compilation with an attribute that has a newline"); |
|---|
| 220 | $tokens = $builder->compile($ctx, '<$MTFoo no="1 |
|---|
| 221 | "$>'); |
|---|
| 222 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 223 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields tokens"); |
|---|
| 224 | ok(@$tokens == 1, "Created 1 token"); |
|---|
| 225 | ok(@{ $tokens->[0] } == 7, "Length of token object is 7 elements"); |
|---|
| 226 | ok($tokens->[0][0] eq 'Foo', "Token 1 is a tag token"); |
|---|
| 227 | ok(ref($tokens->[0][1]) eq 'HASH', "Token 1 has an attribute hashref"); |
|---|
| 228 | is($tokens->[0][1]{no}, "1\n", "Value of 'no' attribute is set properly"); |
|---|
| 229 | is($builder->build($ctx, $tokens), 'no foo', "Building produces expected result"); |
|---|
| 230 | |
|---|
| 231 | diag("Testing conditional tag"); |
|---|
| 232 | $tokens = $builder->compile($ctx, '<MTIfBaz>yes</MTIfBaz>'); |
|---|
| 233 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 234 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields token"); |
|---|
| 235 | is($builder->build($ctx, $tokens, { IfBaz => 1 }), 'yes', "Building with conditional set produces expected result"); |
|---|
| 236 | is($builder->build($ctx, $tokens, { IfBaz => 0 }), '', "Building with conditional unset produces expected result"); |
|---|
| 237 | |
|---|
| 238 | diag("Testing conditional tags with Else tag"); |
|---|
| 239 | $tokens = $builder->compile($ctx, '<MTIfBaz>yes<MTElse>no</MTElse></MTIfBaz>'); |
|---|
| 240 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 241 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields token"); |
|---|
| 242 | is($builder->build($ctx, $tokens, { IfBaz => 1 }), 'yes', "Building with conditional set produces expected result"); |
|---|
| 243 | is($builder->build($ctx, $tokens, { IfBaz => 0 }), 'no', "Building with conditional unset produces expected result"); |
|---|
| 244 | |
|---|
| 245 | diag("Testing conditional tags with Else (but no closing Else) tag"); |
|---|
| 246 | $tokens = $builder->compile($ctx, '<MTIfBaz>yes<MTElse>no</MTIfBaz>'); |
|---|
| 247 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 248 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields token"); |
|---|
| 249 | is($builder->build($ctx, $tokens, { IfBaz => 1 }), 'yes', "Building with conditional set produces expected result"); |
|---|
| 250 | is($builder->build($ctx, $tokens, { IfBaz => 0 }), 'no', "Building with conditional unset produces expected result"); |
|---|
| 251 | |
|---|
| 252 | diag("Testing case-insensitivity for MT templates"); |
|---|
| 253 | $tokens = $builder->compile($ctx, '<$MtFoO$>'); |
|---|
| 254 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 255 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields tokens"); |
|---|
| 256 | ok(@$tokens == 1, "Created one token"); |
|---|
| 257 | ok($tokens->[0][0] eq 'FoO', "Token is a tag token"); |
|---|
| 258 | ok(@{ $tokens->[0] } == 7, "Length of token object is 7 elements"); |
|---|
| 259 | ok(ref($tokens->[0][1]) eq 'HASH', "Element 1 of token object is a hashref"); |
|---|
| 260 | is(scalar keys %{ $tokens->[0][1] }, 0, "Has no attributes"); |
|---|
| 261 | is($builder->build($ctx, $tokens), 'foo', "Building produces expected result"); |
|---|
| 262 | |
|---|
| 263 | diag("Testing optional '$' syntax for function tags"); |
|---|
| 264 | $tokens = $builder->compile($ctx, '<mtfoo>'); |
|---|
| 265 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 266 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields tokens"); |
|---|
| 267 | ok(@$tokens == 1, "Created one token"); |
|---|
| 268 | ok($tokens->[0][0] eq 'foo', "Token is a tag token"); |
|---|
| 269 | ok(@{ $tokens->[0] } == 7, "Length of token object is 7 elements"); |
|---|
| 270 | ok(ref($tokens->[0][1]) eq 'HASH', "Element 1 of token object is a hashref"); |
|---|
| 271 | is(scalar keys %{ $tokens->[0][1] }, 0, "Has no attributes"); |
|---|
| 272 | is($builder->build($ctx, $tokens), 'foo', "Building produces expected result"); |
|---|
| 273 | |
|---|
| 274 | diag("Testing optional namespace ':' syntax for function tags"); |
|---|
| 275 | $tokens = $builder->compile($ctx, '<mt:foo>'); |
|---|
| 276 | diag("Error: " . $builder->errstr) unless $tokens; |
|---|
| 277 | ok($tokens && ref($tokens) eq 'ARRAY', "Compiles and yields tokens"); |
|---|
| 278 | ok(@$tokens == 1, "Created one token"); |
|---|
| 279 | ok($tokens->[0][0] eq 'foo', "Token is a tag token"); |
|---|
| 280 | ok(@{ $tokens->[0] } == 7, "Length of token object is 7 elements"); |
|---|
| 281 | ok(ref($tokens->[0][1]) eq 'HASH', "Element 1 of token object is a hashref"); |
|---|
| 282 | is(scalar keys %{ $tokens->[0][1] }, 0, "Has no attributes"); |
|---|
| 283 | is($builder->build($ctx, $tokens), 'foo', "Building produces expected result"); |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | package My::Context; |
|---|
| 287 | |
|---|
| 288 | use strict; |
|---|
| 289 | use base qw( MT::Template::Context ); |
|---|
| 290 | |
|---|
| 291 | sub new { |
|---|
| 292 | my $class = shift; |
|---|
| 293 | my $ctx = $class->SUPER::new(@_); |
|---|
| 294 | $ctx->{__handlers}{foo} = \&_hdlr_foo; |
|---|
| 295 | $ctx->{__handlers}{bars} = [ \&_hdlr_bars, 1 ]; |
|---|
| 296 | $ctx->{__handlers}{barbaz} = \&_hdlr_bar_baz; |
|---|
| 297 | $ctx->{__handlers}{ifbaz} = [ \&MT::Template::Context::_hdlr_pass_tokens, 1 ]; |
|---|
| 298 | return $ctx; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | sub _hdlr_foo { |
|---|
| 302 | my $args = $_[1]; |
|---|
| 303 | $args && $args->{no} ? 'no foo' : 'foo' |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | sub _hdlr_bars { |
|---|
| 307 | my($ctx) = @_; |
|---|
| 308 | my $tokens = $ctx->stash('tokens'); |
|---|
| 309 | return 'Called without tokens!' if !$tokens || !@$tokens; |
|---|
| 310 | my $builder = $ctx->stash('builder'); |
|---|
| 311 | my $html = ''; |
|---|
| 312 | for (1..2) { |
|---|
| 313 | my $bar = { baz => 'baz' . $_ }; |
|---|
| 314 | $ctx->stash('bar', $bar); |
|---|
| 315 | my $out = $builder->build($ctx, $tokens); |
|---|
| 316 | return $ctx->error( $builder->errstr ) unless defined $out; |
|---|
| 317 | $html .= $out; |
|---|
| 318 | } |
|---|
| 319 | $html; |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | sub _hdlr_bar_baz { |
|---|
| 323 | my $bar = $_[0]->stash('bar'); |
|---|
| 324 | $bar->{baz}; |
|---|
| 325 | } |
|---|