| 1 | # ModifiedSetVarBlock |
|---|
| 2 | # |
|---|
| 3 | # $Id$ |
|---|
| 4 | # |
|---|
| 5 | # This software is provided as-is. You may use it for commercial or |
|---|
| 6 | # personal use. If you distribute it, please keep this notice intact. |
|---|
| 7 | # |
|---|
| 8 | # Copyright (c) 2007 Hirotaka Ogawa |
|---|
| 9 | |
|---|
| 10 | package MT::Plugin::ModifiableSetVarBlock; |
|---|
| 11 | use strict; |
|---|
| 12 | use base qw(MT::Plugin); |
|---|
| 13 | |
|---|
| 14 | use MT; |
|---|
| 15 | use MT::Template::Context; |
|---|
| 16 | |
|---|
| 17 | our $VERSION = '0.01'; |
|---|
| 18 | |
|---|
| 19 | my $plugin = __PACKAGE__->new({ |
|---|
| 20 | id => 'modifiable_set_var_block', |
|---|
| 21 | name => 'ModifiableSetVarBlock', |
|---|
| 22 | description => q(<MT_TRANS phrase="ModifiableSetVarBlock overrides MTSetVarBlock with an alternative version, which can be used with 'modifiers'.">), |
|---|
| 23 | doc_link => 'http://code.as-is.net/wiki/ModifiableSetVarBlock', |
|---|
| 24 | author_name => 'Hirotaka Ogawa', |
|---|
| 25 | author_link => 'http://profile.typekey.com/ogawa/', |
|---|
| 26 | version => $VERSION, |
|---|
| 27 | l10n_class => 'ModifiableSetVarBlock::L10N', |
|---|
| 28 | }); |
|---|
| 29 | MT->add_plugin($plugin); |
|---|
| 30 | |
|---|
| 31 | sub init { |
|---|
| 32 | my $plugin = shift; |
|---|
| 33 | $plugin->SUPER::init(@_); |
|---|
| 34 | my $registry = MT->component('core')->registry('tags', 'block'); |
|---|
| 35 | $registry->{SetVarBlock} = \&_hdlr_set_var_block; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | sub _hdlr_set_var_block { |
|---|
| 39 | my($ctx, $args) = @_; |
|---|
| 40 | my $tag = lc $ctx->stash('tag'); |
|---|
| 41 | my $name = $args->{name} || $args->{var}; |
|---|
| 42 | return $ctx->error(MT->translate( |
|---|
| 43 | "You used a [_1] tag without any arguments.", "<MT$tag>" )) |
|---|
| 44 | unless defined $name; |
|---|
| 45 | |
|---|
| 46 | my $val = MT::Template::Context::_hdlr_pass_tokens(@_); |
|---|
| 47 | return unless defined($val); |
|---|
| 48 | if (my $ph = $ctx->post_process_handler) { |
|---|
| 49 | $val = $ph->($ctx, $args, $val); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | if ($args->{prepend}) { |
|---|
| 53 | my $existing = $ctx->var($name); |
|---|
| 54 | $existing = '' unless defined $existing; |
|---|
| 55 | $val = $val . $existing; |
|---|
| 56 | } elsif ($args->{append}) { |
|---|
| 57 | my $existing = $ctx->var($name); |
|---|
| 58 | $existing = '' unless defined $existing; |
|---|
| 59 | $val = $existing . $val; |
|---|
| 60 | } |
|---|
| 61 | $ctx->var($name, $val); |
|---|
| 62 | return ''; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | 1; |
|---|