Index of /dev/null

utilizing mt context column values

Within Movable Type plugin methods you can access current context column values through the context stash.

See for a rudimental introduction to the concept of template context.

sub handler {
  my ($ctx, $args, $cond) = @_;

  if ( my $entry_stash = $ctx->stash('entry') )
    # access the accordant mt_entry row, where keys are
    # column names w/o prefix, i.e. id, bog_id, etc.
    my $entry = $entry_stash->{column_values};

    if ( my $comment_stash = $ctx->stash('comment') )
      my $comment = $comment_stash->{column_values};
      # mt_comment column values

    # more stash blocks ...


For instance, consider a custom entry template tag usage like this:

<mt:MyTemplateTag caption="<$mt:EntryTitle$> authored by <$mt:EntryAuthor$>">

This won't work as expected, since MT doesn't substitite template tags within template tag attributes. So, using context stash, you can insert a simple template tag substitution into plugin code.

sub MyTemplateTag {
  my ($ctx, $args, $cond) = @_;

  # some template tag attribute
  my $caption = $args->{caption};

  # check for column groups within context stash
  my %columns = map {
    my $s = $ctx->stash($_);
    $_ => (ref $s ? $s->{column_values} : undef);
  } qw(entry comment ping);

  # substitute <$mt:FooBarName$> with the column
  # value of bar_name in table mt_foo
  local *subst = sub {
    my ($match, $name) = @_;
    foreach ( keys %columns )
      # map FooBarName to column bar_name in mt_foo
      $name =~ /^$_(\w+)/i or next;
      my $key = $1;
      $key =~ s/([a-z])([A-Z])/qq($1_\l$2)/eg;
      $key = lc $key;
      return $columns{$_}{$key}
        if exists $columns{$_}{$key};
    $match; # not recognized
  $caption =~ s/(<\$mt:(\w+)\s*\$>)/&subst($1,$2)/egi;

  # now do something interesting with $caption ...

of course, only template tags without modifiers are recognized.

no comments

post a comment

Leave a comment
You may use HTML tags for style