[Templates] ANNOUNCE: Template-Plugin-HTML-Template

darren chamberlain dlc@users.sourceforge.net
Wed, 25 Sep 2002 12:44:23 -0400


* Tatsuhiko Miyagawa <miyagawa@edge.co.jp> [2002-09-25 12:02]:
> Just an announcement of scary plugin: Template::Plugin::HTML::Template!

You know, I wrote this exact plugin a few months ago, and just never
released it.

> Well, it uses following hacking code to get stash variable keys.
> 
> my $dont_use = qr/^(?:global|component|HTML|_DEBUG|_PARENT|dec|template)$/;
> 
>     # XXX there should be a better way
>     my $stash = $self->{_CONTEXT}->stash;
>     my @keys = grep !/$dont_use/, keys %{$stash};
> 
> 
> Is there any better way than this?

My plugin uses:

      @params = map { ($_ => $stash->{ $_ }) } grep !/^[\._]/, keys %$stash;

Which is what _dotop uses to determine whether a variable is valid to
return, i.e., the elements of %$stash that are actual variables and not
part of the instance.

FWIW, my implementation looks like:

  use base qw(Template::Plugin::Filter);
  $DYNAMIC = 1;

  sub filter {
      my ($self, $text, undef, $conf) = @_;
      my ($stash, @params, $t);

      $stash = $self->{ _CONTEXT }->stash;
      @params = map { ($_ => $stash->{ $_ }) } grep !/^[\._]/, keys %$stash;
      $conf = $self->merge_conf($conf);

      $t = HTML::Template->new(die_on_bad_params => 0,
                               strict            => 0
                               %$conf,
                               scalarref         => \$text);

      $t->param(@params);
      return $t->output;
  }

It's mostly identical to your own, except for the lack of the named
filter and the addition of the $conf parameter.  This last means you can
do:

    [% USE ht = HTML.Template(cache => 1, die_on_bad_params => 1) %]
    [% FILTER $ht %]
        Hello, <TMPL_VAR NAME=WHAT>!
    [% END %]

And the extra parameters get passed to the HTML::Template constructor.
Very useful in combination with disk-based caching.

Now I'm going to have to upload all my plugins, before you rewrite them!

(darren)

-- 
The smart way to keep people passive and obedient is to strictly limit
the spectrum of acceptable opinion, but allow very lively debate
within that spectrum.
    -- Noam Chomsky