| … | |
… | |
| 346 | } |
346 | } |
| 347 | |
347 | |
| 348 | # This is different. It probably should print anything (except in debugging cases) |
348 | # This is different. It probably should print anything (except in debugging cases) |
| 349 | # and it should return a boolean, not a string. &if is called in a nonstandard way |
349 | # and it should return a boolean, not a string. &if is called in a nonstandard way |
| 350 | # by &template, with $args as an arrayref instead of a hashref. this is a hack! yay! |
350 | # by &template, with $args as an arrayref instead of a hashref. this is a hack! yay! |
|
|
351 | |
|
|
352 | # OK, this is a pluggin architecture. it iterates through attributes of the "if" tag, |
|
|
353 | # and for each predicate $p, it calls &if_$p in an object-oriented way, continuing the |
|
|
354 | # grand templating theme of an object-oriented pluggable architecture using ->can($). |
| 351 | sub if { |
355 | sub if { |
| 352 | my ($self, $args) = @_[0,-1]; |
356 | my ($self, $args) = @_[0,-1]; |
| 353 | # A single if "or"s it's components. Nesting produces "and". |
357 | # A single if "or"s it's components. Nesting produces "and". |
| 354 | |
358 | |
| 355 | my @args = @$args; # Hahahahaha, get it?! |
359 | my @args = @$args; # Hahahahaha, get it?! |
| … | |
… | |
| 360 | } |
364 | } |
| 361 | |
365 | |
| 362 | while (@args > 1) { |
366 | while (@args > 1) { |
| 363 | my ($key, $value) = (shift @args, shift @args); |
367 | my ($key, $value) = (shift @args, shift @args); |
| 364 | |
368 | |
| 365 | if ($key eq "can" and $self->can($value)) { |
369 | # a non-existent &if_$key is the same as a false result, but we're ORing, so it's OK |
|
|
370 | my $sub = "if_$key"; # perl doesn't like it when you try to construct a string right in a method invocation |
|
|
371 | if ($self->can("if_$key") and $self->$sub("$value")) { |
| 366 | return 1; |
372 | return 1; |
| 367 | } |
373 | } |
| 368 | |
|
|
| 369 | # Other conditions go here, friend. |
|
|
| 370 | } |
374 | } |
| 371 | |
375 | |
| 372 | return 0; |
376 | return 0; |
|
|
377 | } |
|
|
378 | |
|
|
379 | # &if_can will return 1 if the current object->can("do $_[1]") |
|
|
380 | sub if_can ($$) { |
|
|
381 | my ($self, $arg) = (@_); |
|
|
382 | |
|
|
383 | if ($self->can("$arg")) { |
|
|
384 | return 1; |
|
|
385 | } else { |
|
|
386 | return 0; |
|
|
387 | } |
| 373 | } |
388 | } |
| 374 | |
389 | |
| 375 | 1; |
390 | 1; |
| 376 | |
391 | |
| 377 | __END__ |
392 | __END__ |