beta BLOG dot NET

/* bugs, features, drafts, and solutions. */

// utilizing mt context …

Sebastian blogged on 2009-08-29T16:56:01+00:00

utilizing mt context column values


Within Movable Type plugin methods you can access current context column values through the context stash. See http://www.movabletype.org/documentation/developer/the-template-context.html for a rudimental introduction to the concept of template context.
[-] hide code
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:
[-] hide code
<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.
[-] hide 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.

# 

$tags

 = [  ];
# 

$categories

 = [  ];
# 

no comments

, 

no trackbacks

→ leave a comment


# 

trackback_url

( http://mt42.beta-blog.net/script/trackback/194080 );
here goes the message.