Franco Bagnoli wrote:
> I can only use the DBI plugin. 

Here's an easier way.

  package Template::Provider::Allow;
  use Template::Constants qw( STATUS_DECLINED STATUS_ERROR );

  sub new {
      my $class = shift;
      bless { map { ($_, 1) } @_ }, $class;
  }

  sub fetch {
      my $self = shift;
      my $name = shift;
      return $self->{ $name } 
          ? (undef,  STATUS_DECLINED)
          : ("access to $name not allowed", STATUS_ERROR);
  }

This provider is initialised with the names of plugins (or any resource)
that are allowed.  We also need the regular plugins provider.

  my $allow    = Template::Provider::Allow->new(qw( Date Table ));
  my $plugins  = Template::Plugins->new();

Then we define the LOAD_PLUGINS chain-of-command with the Allow 
provider first.  

  my $template = Template->new( LOAD_PLUGINS => [ $allow, $plugins ]);

If the plugin is allowed then the Allow provider returns DECLINED and
control passes to the regular plugins provider.  Otherwise the Allow
provider returns an error.

Here it is in use.

  [% TRY;
       USE Date;
       "got date\n";
     CATCH;
       "not date: $error\n";
     END;
  
     TRY;
       USE Table([1, 2, 3]);
       "got table\n";
     CATCH;
       "not table: $error\n";
     END;
  
     TRY;
       USE Format;
       "got format";
     CATCH;
       "not format: $error\n";
     END;
  %] 

Here's the output:

  got date
  got table
  not format: plugin error - access to Format not allowed

Template::Provider::Disallow is left as an excercise for the 
reader...  :-)

HTH

A

 


_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to